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
0 件のコメント
採用された回答
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 件のコメント
Image Analyst
2016 年 7 月 4 日
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'})
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Colormaps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!