簡體   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