2017-10-11 59 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