在图例中移动线条

时间:2017-10-11 11:53:36

标签: matlab graph matlab-figure legend

我有一个包含四个字符串的单元格数组,用作四个单独的X,Y图的图例。一个字符串非常长,因此被sprintf划分为四行图例。四个图例如下图所示。是否可以将蓝线向上移动,使其适合第一条线,即第四条线。'

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);

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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