2017-10-11 18 views
2

私は4つの個々のX、Yプロットの凡例として使用される4つの文字列を持つセル配列を持っています。 1つの文字列は非常に長く、したがってsprintfによって4行の凡例に分割されます.4つのプロットの凡例が下の図に示されています。青い線を上に移動して、「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