# Scatter plot legend for only two of the three plotted categories

24 visualizzazioni (ultimi 30 giorni)
Jonne Klockars il 4 Giu 2022
Commentato: Voss il 6 Giu 2022
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? Everything else in the code runs nicely. 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')
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Voss il 4 Giu 2022
scatter creates a single scatter object, so you won't be able to make multiple legend entries from it, but you can create additional lines to use in the legend (i.e., lines with NaN data so they don't show up in the plot, but they have the right properties - color, marker, etc. - to be used in the legend).
% random data:
stock2 = rand(1738,6);
figure
middle = stock2;
[~,j] = sort(stock2(:,6),'ascend');
% I imagine this is how high100 and low100 are defined:
high100 = j(end-99:end);
low100 = j(1:100);
remove = j([1:100 end-99:end],:);
middle(remove,:)=[];
% color map (simplified construction)
c = zeros(size(stock2,1),3);
n_middle = size(middle,1);
c(1:n_middle,2) = 1; % green
c(n_middle+(1:100),1) = 1; % red
c(n_middle+size(high100,1)+(1:100),3) = 1; % blue
scatter(stock2(:,5),[middle(:,6); stock2(high100,6); stock2(low100,6)],[],c,'.')
hold on
% plot two NaN lines for the legend
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);
title('Stock2')
xlabel('Closing Price')
ylabel('Volume')
"why is there a [ ] in the scatter plot when using a color map?"
The third input to scatter is the marker size, and you have to put something there if you want to use subsequent inputs, such as color and/or marker type. If you specify empty marker size, then MATLAB evidently uses the default value.
##### 7 CommentiMostra 5 commenti meno recentiNascondi 5 commenti meno recenti
Jonne Klockars il 6 Giu 2022
Ok, thank you very much for the lesson!
Voss il 6 Giu 2022
You're welcome!

Accedi per commentare.

### Categorie

Scopri di più su Legend in Help Center e File Exchange

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by