Legend/colorbar for scatterplot with colour-coded subject-wise markers

조회 수: 7 (최근 30일)
I am simplifying my problem to make it easier to answer. Say I have ratings on two measures, x and y, from N subjects. I would like to do a scatterplot of x and y with a different marker colour for each subject, and display a colorbar/legend that shows what colour corresponds to which subject. I have the following code, that I hoped would assign a random colour to each subject across a given colour-space:
N_subj = 30;
X = rand(30,20);
Y = rand(30,20);
for i_subj=1:N_subj
x = X(i_subj,:);
y = Y(i_subj,:);
hold on
markerColour = i_subj/N_subj;
markerColour = [markerColour markerColour markerColour];
colormap summer
h(i_subj)=scatter (x, y, ...
'MarkerEdgeColor','black',...
'MarkerFaceColor',markerColour);
end
colorbar
However, this does not happen so in the code above I instead chose increasingly dark shades of grey. Problem, is the colorbar command displays an irrelevant (yellow-to-red) colour bar, instead of the shades of grey that I used.
If I use legend instead of colorbar, the colour associations are correct but the legend entries are discrete whereas I;d like the mto be continous (stacked rectangles).
Any recommendations? Many thanks

채택된 답변

Image Analyst
Image Analyst 2016년 7월 2일
When you say
markerColour = [markerColour markerColour markerColour];
R, G, and B are all the same - thus your color is some darkness of gray. Perhaps you want this:
N_subj = 30;
X = rand(30,20);
Y = rand(30,20);
% Create colormap with N_subj colors.
summerColorMap = summer(N_subj);
for i_subj=1:N_subj
x = X(i_subj,:);
y = Y(i_subj,:);
hold on
% Get the color for this subject from the summer colormap.
markerColour = summerColorMap(i_subj,:);
h(i_subj)=scatter (x, y, ...
'MarkerEdgeColor','black',...
'MarkerFaceColor',markerColour);
end
% Display colorbar.
colormap(summerColorMap);
colorbar
  댓글 수: 3
z8080
z8080 2016년 7월 4일
Any thoughts on these two follow up questions? Many thanks!
Image Analyst
Image Analyst 2016년 7월 4일
Try lines(), colorcube(), prism(), or hsv(). See the list of them in the help for colormap.
Or see this link, or make your own.
To label the colorbar, see the help for colorbar where it shows this example:
colorbar('Ticks',[-5,-2,1,4,7],...
'TickLabels',{'Cold','Cool','Neutral','Warm','Hot'})

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by