【问题标题】:Move line in legend在图例中移动线
【发布时间】:2018-03-23 02:10:15
【问题描述】:

我有一个包含四个字符串的元胞数组,用作四个单独的 X、Y 图的图例。一个字符串很长,因此被sprintf分成四行图例,四行图例如下图所示。是否可以将蓝线向上移动,使其适合第一行,即“Av”旁边。

这是一个简短的代码示例:

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

【问题讨论】:

  • 请添加您用来生成图例的代码(一个小例子)
  • 嗨路易斯。我在上面的问题中包含了一个小代码,谢谢

标签: matlab graph matlab-figure legend


【解决方案1】:

我做了一些解决方法并找到了这种方式: 在图例中创建与您的字符串数量一样多的行并使它们不可见。

% 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'})

【讨论】:

    【解决方案2】:

    这是一个快速而简单的技巧:将多行字符串拆分为不同的字符串,并让多余的行出现在图例中,而没有关联的可见行。

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

    【讨论】:

    • 你又快了!你觉得我现在的回答还有意义吗?
    • @Mikhail_Sam 哦,我不知道它在做类似的事情。我看到了没有 cmets 的以前版本的你的答案,并且代码没有按原样运行(x 未定义)。是的,我认为值得保留,因为它使用了稍微不同的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多