简体   繁体   中英

MATLAB scatter plot legend

I have a 1738x6 matrix ( stock2 ), from which I plotted the 6th column (y-axis) and the 5th column (x-axis). I divided values from the 6th column to three categories; top 100 (red dots), bottom 100 (blue dots) and the rest (green dots). I have extracted these high and low values, they are called high100 and low100 in the code below.

I understand that I have only one y-value in the plot and that it contains the three different categories. But I can't find a way to create a legend for the plot so that it would show only red dots and blue dots from inside my y-value. All attempts either fail or show a green dot and the first label of the legend. Could someone kindly show how to create the desired legend? And as an extra question: why is there a [] in the scatter plot when using a color map?

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')

I simulated your idea on random data. You could check out documentation on hold , scatter and legend .

The brackets is in the place for size parameter, likely the default value were used then.

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');

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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