繁体   English   中英

MATLAB 散点图 plot 图例

[英]MATLAB scatter plot legend

我有一个 1738x6 矩阵 ( stock2 ),我从中绘制了第 6 列(y 轴)和第 5 列(x 轴)。 我将第 6 列的值分为三类; 前 100 名(红点)、后 100 名(蓝点)和 rest(绿点)。 我已经提取了这些高值和低值,它们在下面的代码中称为high100low100

我知道我在 plot 中只有一个 y 值,它包含三个不同的类别。 但我找不到为 plot 创建图例的方法,以便它只显示我的 y 值内的红点和蓝点。 所有尝试要么失败,要么显示绿点和图例的第一个 label。 有人可以展示如何创建所需的图例吗? 作为一个额外的问题:为什么在使用颜色 map 时散点图 plot 中有一个[]

figure
% color map
c = zeros(size(stock2,1),3);
middle = stock2;
[~,j] = sort(stock2(:,6),'ascend');
remove = j([1:100 end-99:end],:);
middle(remove,:)=[];
% other points are green so blue and red can be easily distinguished
% blue didn't seem to stand out from the default black dots
d=length(middle);
for i=1:d
    c(i,2)=1;
end
% red
a=length(middle)+1;
aa=a+99;
for i=a:aa
    c(i,1)=1;
end
% blue
b=length(middle)+length(high100)+1;
bb=b+99;
for i=b:bb
    c(i,3)=1;
end

scatter(stock2(:,5),[middle(:,6); stock2(high100,6); stock2(low100,6)],[],c,'.')
title('Stock2')
xlabel('Closing Price')
ylabel('Volume')
legend('100 highest volume days','100 lowest volume days')

我在随机数据上模拟了你的想法。 您可以查看有关holdscatterlegend的文档。

括号位于大小参数的位置,可能当时使用了默认值。

stock2=sortrows(rand(300,6),6,'descend');
figure()
h=scatter(reshape(stock2(:,5),100,[]),reshape(stock2(:,6),100,[]),'.');
[h.MarkerEdgeColor]=deal('b','g','r');
title('Stock2')
xlabel('Closing Price')
ylabel('Volume')
legend([h(1),h(3)],{'100 highest volume days','100 lowest volume days'}, ... 
    'Location','northoutside','Orientation','horizontal');

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM