2017-04-13 28 views
0

我想知道是否可以將圖例項與圖例項分離爲兩種不同類型的行。例如:假設您有4條曲線:純黑,純紅,虛線黑,紅色虛線。黑色曲線描述黑色現象,紅色曲線描述現象紅色。實線決定我們是否添加除實體之外的其他貢獻,虛線表示我們爲它添加一些虛線貢獻。在我的情節的傳說中,我只想要兩個條目:現象黑色或現象紅色。但我希望每個條目的傳奇線分成兩部分:上半部分是堅實的,後半部分是破滅。以同樣的方式,是否有可能以相反的方式做到這一點(一半是純黑色,另一半是純紅色,另一半是半黑色半紅色)。是否可以在Matlab中將圖例輸入行分成兩部分

對於4條曲線,這並沒有多大意義。但我有時不得不把6點或8的曲線和傳說是那麼太大,可以把它放在圖中的...

目前我使用這行添加我的傳說: legend({str1,str2},'Interpreter','latex') 但我不不知道這是否有意義。

我張貼的圖片來說明我想什麼(請注意,這可能是周圍的其他方法,有兩種風格的一條線,而不是兩種顏色):enter image description here

回答

0

這不正是你要的,但它的另一種方法:

styles = {'-','--'}; 
colors = {'r','g','b'}; 
colorNames = {'red','green','blue'}; 
styleNames = {'normal','dashed'}; 
hold on 
% plot many lines 
for ii = 1:numel(styles) 
    for jj = 1:numel(colors) 
     plot((1:10) + jj + ii*numel(colors),'Color',colors{jj},'LineStyle',styles{ii}) 
    end 
end 
% generate handles for the legend 
h = []; 
for ii = numel(colors):-1:1 
    h(numel(styles)+ ii) = plot(0,0,'Color',colors{ii},'LineStyle','-'); 
end 
for ii = numel(styles):-1:1 
    h(ii) = plot(0,0,'Color','k','LineStyle',styles{ii}); 
end 
hold off 
legend(h,[styleNames colorNames]); 

enter image description here

0

沒有在爲Matlab的傳說做這個內置的能力。您可以通過手動繪製線條來實現類似的效果。這使用了annotation arrow功能爲一個圖:

% plot some dummy data (not connected to the manual legend! 
x = linspace(-1,1); 
clf; hold on; grid on 
% Set up linestyles and linecolors here so that they can be (at least 
% slightly) linked between the plot and the manual legend. 
linestyles = {'-', '--'}; 
linecolors = {'k', 'r'}; 
% plots 
plot(x,x.^2,'linestyle',linestyles{1},'color',linecolors{1}); 
plot(x,x.^3,'linestyle',linestyles{1},'color',linecolors{2});  
plot(x,x.^4,'linestyle',linestyles{2},'color',linecolors{1}); 
plot(x,x.^5,'linestyle',linestyles{2},'color',linecolors{2}); 
% scale the plot within the figure to leave room for legend 
plotsize = [0.06, 0.20, 0.9, 0.75]; 
set(gca,'position', plotsize) 
% x and y are original positions for the lines 
x = 0.4; y = 0.1; 
% dx and dy are length and vertical spacing of lines respectively 
dx = 0.1; dy = 0.05; 

% The main event: drawing (headless) text arrows, so that one of them can have 
% a string properly which is your legend entry label. Use x,y,dx,dy for positioning 
annotation('textarrow', [x,x+dx], [y,y], ... 
    'linestyle', linestyles{1}, 'color', linecolors{1}, 'textcolor', 'k', 'headstyle', 'none', ... 
    'string', 'Even functions ') 
annotation('textarrow', [x+dx + 0.005,x+2*dx + 0.005], [y,y], ... 
    'linestyle', linestyles{2}, 'color', linecolors{1}, 'textcolor', 'k', 'headstyle', 'none') 
annotation('textarrow', [x,x+dx], [y-dy,y-dy], ... 
    'linestyle', linestyles{1}, 'color', linecolors{2}, 'textcolor', 'k', 'headstyle', 'none', ... 
    'string', 'Odd functions ') 
annotation('textarrow', [x+dx + 0.005,x+2*dx + 0.005], [y-dy,y-dy], ... 
    'linestyle', linestyles{2}, 'color', linecolors{2}, 'textcolor', 'k', 'headstyle', 'none') 

結果:

plot

注意該定位與歸一化的完成的值(0和1之間),因此它們與舒展圖。如果您的繪圖具有固定大小,處理像素可以更容易可視化,這可以通過在調整大小時更改各種圖形對象的參數來完成(請參閱上面關於註釋箭頭鏈接的文檔)。

+0

有沒有辦法知道圖例本身的屬性?例如,圖例中繪製的線的大小,圖例的周圍框與線條本身之間的空間,......類似的東西,以便我至少可以獲得一個位置,從中可以繪製出像您這樣的註釋沒有。我無法自己找到它,但我想我有很多我不知道matlab,必須有一種方法來找到這些參數。 – mwoua

+0

總之,我不這麼認爲。 – Wolfie

相關問題