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')
CodePudding user response:
I simulated your idea on random data. You could check out documentation on
CodePudding user response:
Using
h = [ ...
plot(NaN,NaN,'.','Color',c(end-100,:),'DisplayName','100 highest volume days') ... % red
plot(NaN,NaN,'.','Color',c(end,:),'DisplayName','100 lowest volume days') ... % blue
];
legend(h);
produces the legend I wanted for the single scatter plot. Suggested by Voss, requires Simulink: https://se.mathworks.com/matlabcentral/answers/1733900-scatter-plot-legend-for-only-two-of-the-three-plotted-categories
X Zhang's answer works also, but it uses several scatter plots.