Effacer les filtres
Effacer les filtres

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

12 vues (au cours des 30 derniers jours)
z8080
z8080 le 2 Juil 2016
Commenté : Image Analyst le 4 Juil 2016
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

Réponse acceptée

Image Analyst
Image Analyst le 2 Juil 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 commentaires
z8080
z8080 le 4 Juil 2016
Any thoughts on these two follow up questions? Many thanks!
Image Analyst
Image Analyst le 4 Juil 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'})

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by