2015-03-02 75 views
1

出於某種原因,我想單獨繪製相同數據的直線和標記。多個圖的一個組合圖例條目

data1 = (1:1:10)'; 
data2 = (1:2:10); 
figure(1); 
plot(data1,data1,'or'); 
hold on; 
plot(data2,data2,'-r'); 
legend('data'); 

但是它只會顯示第一個情節的傳說。而Matlab似乎沒有選擇來操縱圖例標記,顏色和線條樣式。 enter image description here

我該如何製作這樣的傳奇?

enter image description here

謝謝!

+1

你可以操縱有關使用輸出參數傳說什麼。當調用'legend'時像's'這樣的'icons'變量:'[h,icons,plots,str] = legend(___)' – 2015-03-02 20:39:54

+1

Thanks @ Benoit_11!這正是我正在尋找的 – cgao 2015-03-02 20:54:58

+0

太棒了!我必須說,我不明白你的問題,除了「操縱傳奇標記部分」哈哈:) – 2015-03-02 20:56:55

回答

5

你需要繪製一個無形第三幅圖(幾乎沒有數據保持快速)來定義你的傳說:

data1 = (1:1:10)'; 
data2 = (1:2:10); 
figure(1); 
plot(data1,data1,'or'); hold on 
plot(data2,data2,'-r'); hold on 

%// legend plot 
lp = plot(0,0,'-r','Marker','o','visible','off') 
legend(lp,'data'); 

enter image description here

你需要傳遞的手柄legend命令的無形劇情,或者您甚至可以將隱形劇情放入圖例中:

legend(plot(0,0,'-r','Marker','o','visible','off'),'data'); 

如果你需要更多的時候,你可以寫一個小幫手功能

style = @(LineStyle, MarkerStyle) plot(0,0,LineStyle,'Marker',MarkerStyle,'visible','off') 
legend(style('-r','o'),'data'); 

...您可以與'color''LineWidth'或任何你想定製。

它使您可以將多個獨立的條目從您的實際數據創建完全定製的傳說:

legend([style('-r','o'),style('-b','x'),style('-g','v')],{'1','2','3'}); 

enter image description here

+0

謝謝!它避免了直接操縱圖例的圖標。 – cgao 2015-03-02 22:11:59

+1

非常好。 BTW恭喜10K! – rayryeng 2015-03-02 22:15:19

+0

@rayryeng謝謝;) – thewaywewalk 2015-03-02 22:19:39