Legend/colorbar for scatterplot with colour-coded subject-wise markers
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Image Analyst
il 2 Lug 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 Commenti
Image Analyst
il 4 Lug 2016
Try lines(), colorcube(), prism(), or hsv(). See the list of them in the help for colormap.
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'})
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Colormaps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!