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

6 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 2 Jul 2016
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 Comments
Image Analyst
Image Analyst on 4 Jul 2016
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'})

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!