【问题标题】:Matlab: Reduce the spacing between symbols and labels in the legendMatlab:减少图例中符号和标签之间的间距
【发布时间】:2013-06-28 01:28:21
【问题描述】:

在matlab中使用legend命令时,如何减小图例符号与其对应标签之间的水平距离?

示例代码:

Line1=plot(x1,y1,'s');
Line2=plot(x2,y2,'o');
Line3=plot(x3,y3,'^');
Leg=legend([Line1, Line2, Line3],...
           'Line1 text','Line2 text','Line3 text',...
           'Location','NorthEast');

【问题讨论】:

    标签: matlab legend matlab-figure legend-properties


    【解决方案1】:

    您可以找到Leg 的子代,搜索将Type 设置为text 的子代并重新定位它们。这是一个代码来展示如何做到这一点。它将它们向左移动 0.2,相对于图例框。

    ch = get(Leg, 'Children');
    textCh = ch(strcmp(get(ch, 'Type'), 'text'));
    for iText = 1:numel(textCh)
        set(textCh(iText), 'Position', get(textCh(iText), 'Position') + [-0.2 0 0])
    end
    

    【讨论】:

    • 谢谢!这正是我想做的。
    【解决方案2】:

    我很好奇您为什么要这样做,但可能的解决方案是:

    clf;
    hold on;
    x=0:0.1:2*pi;
    plot(x,sin(x),'s');
    plot(x,cos(x),'o');
    ax=legend('sin','cos');
    LEG = findobj(ax,'type','text');
    set(LEG,'HorizontalAlignment','center')
    

    您可以测试 'center''right' 并使用任何可行的方法。如果两者都不起作用,请忽略我的回答。

    【讨论】:

    • 谢谢,好主意,但这会导致图例文本与符号重叠。我只是想提高我的情节的可读性,我觉得就目前而言,图例文本和符号之间的空间太大
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2020-03-09
    • 2019-03-22
    相关资源
    最近更新 更多