2017-10-11 155 views
2

我有一個單元陣列,有四個字符串,用作四個單獨的X,Y圖的圖例。一個字符串很長,因此劃分爲四行的圖例sprintf,下圖顯示了四個圖例。是否有可能將藍線向上移動,以便符合「Av」旁邊的第一條線。在圖例中移動線條

enter image description here

這裏是簡短的代碼示例:

X=[2 4 6 8; 2 3 4 5; 4 5 6 7 ; 7 6 8 9]; 
Y=[1 3 5 7; 2 5 6 8; 8 6 4 2; 7 5 4 3]; 

Title = { 
'123456789_1' 
'ABCDEFGHIJ_1' 
'123ABC_1' 
sprintf('Av. \n(123456789_1 \nABCDEFGHIJ_1 \n123ABC_1)') 
}; 

fig1=figure; 
hold on 
for i=1:size(X,2) 
plot(X(:,i),Y(:,i)); 
end 
hold off 
legend(Title,'Orientation','vertical','Location','northeastoutside','FontSize',8); 
+0

請加入用來生成傳說(一個小例子) –

+0

路易斯喜的代碼。我在上面的問題中包含了一個小代碼,謝謝 –

回答

1

這裏有一個快速和骯髒的伎倆:多行字符串分割成不同的字符串,並有多餘的線條出現在沒有關聯可見線的圖例。

X=[2 4 6 8; 2 3 4 5; 4 5 6 7 ; 7 6 8 9]; 
Y=[1 3 5 7; 2 5 6 8; 8 6 4 2; 7 5 4 3]; 

Title = { 
'123456789_1' 
'ABCDEFGHIJ_1' 
'123ABC_1' 
'Av.' % split into different strings 
'(123456789_1 ' 
'ABCDEFGHIJ_1' 
'123ABC_1)' 
}; 

fig1=figure; 
hold on 
for i=1:size(X,2) 
plot(X(:,i),Y(:,i)); 
end 
for k=1:3 % 3 is the number of extra lines. Manually set 
    plot(NaN,'color','none') % plot invisible lines with no color, will 
          % generate legend entries 
end 
hold off 
legend(Title,'Orientation','vertical','Location','northeastoutside','FontSize',8); 

enter image description here

+0

你又快了!你認爲我的答案現在有什麼意義嗎? –

+0

@Mikhail_Sam哦,我不知道它在做類似的事情。我看到以前版本的yur回答沒有註釋,並且代碼不按原樣運行('x'未定義)。是的,我認爲值得保留,因爲它採用了稍微不同的方法 –

2

我做了一些解決方法,發現是這樣的:在 傳說創造這麼多的線路作爲自己的串號,並使其不可見。

% data example 
x = [1:0.1:6.2] 
% create plot. Let them be nan - they will not be shown at plot 
plot(x, [x.^2; x.^3; x.^4; x.^5; nan(size(x)); nan(size(x)); nan(size(x))]) 
% create legend 
[~,iconsH] = legend('f1','f2','f3','my','text','is','here'); 
% find picture of legend and make lines with such Tags invisible 
cellfun(@(x) set(findobj(iconsH, 'Tag', x),'Vis','off'), {'text','is','here'}) 

enter image description here