Matlab: How to set color of legend in a scatter pl

2019-07-16 23:43发布

Here is the sample code:

x = rand(100,1);
y = rand(100,1);
score = rand(100,1);
figure;
colormap(flipud(bone));
caxis([0 1])
axis([0 1 0 1])
scatter(x,y,50,score,'+','LineWidth',2);
legend('scores');

I'm using the reversed 'bone' colormap which assigns pure white to score value 0 and pure black to score value 1. However, the legend seems to be automatically assigned the score 0, and so if you run the code the legend color is pure white and not visible.

Is there any ways to fix that? Thanks.

2条回答
Deceive 欺骗
2楼-- · 2019-07-17 00:15

What you are looking for is a colorbar. This shows a bar with color gradient for all values in the colormap used.

Replace legend('scores'); line with colorbar.

P.S. legend is meant for identifying multiple plots in the same figure.

查看更多
男人必须洒脱
3楼-- · 2019-07-17 00:16

If you only want to plot a black + without showing the color range of the data (as with color bar) you can create a dummy legend for that. Here is how you do that:

% plot some dummy data for the legend:
scatter(nan,nan,[],1,'+','LineWidth',2)
hold on
% plot your data:
scatter(x,y,50,score,'+','LineWidth',2);
hold off
% add the legend only for the first (dummy) data:
leg = legend('scores');

The result:

enter image description here

查看更多
登录 后发表回答