【问题标题】:How to show legend for only a specific subset of curves in the plotting?如何在绘图中仅显示特定曲线子集的图例?
【发布时间】:2012-11-21 01:54:00
【问题描述】:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);

hold on;
plot(t, s, 'r');
plot(t, c, 'b');
plot(t, m, 'g');
hold off;

legend('', 'cosine', '');

我的绘图中有几条曲线。我只想显示其中一些的图例。我该怎么做?

例如,如何在上面的绘图中只显示余弦曲线的图例?当我将legend() 函数称为legend('', 'cosine'); 而不是添加空的第三个参数时,实际上第三条绿线已从图例中删除。但这并不能解决我的问题,因为不需要的红线仍然可见。

【问题讨论】:

    标签: matlab plot legend


    【解决方案1】:

    我不喜欢存储句柄值,当我的图中有很多图表时,它会变得一团糟。因此我找到了另一个解决方案。

    t = 0 : 0.01 : 2 * pi;
    s = sin(t);
    c = cos(t);
    m = -sin(t);
    hold on;
    plot(t, s, 'r', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
    h2 = plot(t, c, 'b', 'DisplayName', 'cosine');  % Plotting and giving legend name
    plot(t, m, 'g', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
    
    legend show  % Generating legend based on already submitted values
    

    这给了我与 Eitan T 的答案相同的图表。

    需要注意的是,这也会影响其他 matlab 函数,例如 cla 只会删除图例中提到的绘图。在 Matlab 文档中搜索 HandleVisibility 以了解更多信息。

    【讨论】:

    • 也使用了这个,因为我使用动态命名的曲线(绘制标准偏差效果很好,但将它们隐藏在图例中)。
    • 如果想直接在 UI 中进行更改,最好的解决方案。
    【解决方案2】:

    只需将所需的图例句柄存储在一个变量中,然后将数组传递给legend。在您的情况下,它只是一个值,如下所示:

    hold on;
    plot(t, s, 'r');
    h2 = plot(t, c, 'b');  % # Storing only the desired handle
    plot(t, m, 'g');
    hold off;
    
    legend(h2, 'cosine');  % # Passing only the desired handle
    

    你应该得到这个情节:

    【讨论】:

    • 请注意,使用这种方法,一旦您关闭图例并通过 UI 将其重新打开,所有行都将返回到图例中。
    • PNG 更适合这种图像。
    • 附注:这基本上是mathworks.com/help/matlab/creating_plots/…推荐的内容
    【解决方案3】:

    让我们从变量开始并绘制它们:

    t = 0 : 0.01 : 2 * pi;
    s = sin(t);
    c = cos(t);
    m = -sin(t);
    
    figure;
    hold ('all');
    hs = plot(t, s);
    hc = plot(t, c);
    hm = plot(t, m);
    

    有一个名为IconDisplayStyle 的属性。它埋得很深。您需要遵循的路径是:

    行 -> 注释 -> LegendInformation -> IconDisplayStyle

    设置IconDisplayStyle 属性off 将让您跳过该行。例如,我将关闭hs的图例。

    hsAnno = get(hs, 'Annotation');
    hsLegend = get(hsAnno, 'LegendInformation');
    set(hsLegend, 'IconDisplayStyle', 'off');
    

    当然你可以继续这样做:

    set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
    

    但我发现它更难理解。

    现在,legend 函数将跳过 hs

    以此结束我的代码:

    legend('cosine', 'repeat for this handle')
    

    会给你这个:

    编辑:乔纳斯在 cmets 中有一个很好的建议: 像这样设置 hc 的DisplayName 属性:

    set(hc, 'DisplayName', 'cosine');
    legend(gca, 'show');
    

    将为您提供所需的图例。您将把您的线路句柄与'cosine' 关联起来。因此,您可以使用'off''show' 参数调用图例。

    【讨论】:

    • 我建议设置线句柄的DisplayName 属性,而不是使用名称调用legend,以便在GUI 中关闭/打开图例后结果将相同。
    • 谢谢@Jonas。更新了我的答案。
    【解决方案4】:

    您可以更改绘制曲线的顺序并将图例应用于第一条曲线:

    t = 0 : 0.01 : 2 * pi;
    s = sin(t);
    c = cos(t);
    m = -sin(t);
    
    plot(t,c,t,s,t,m)  % cosine is plotted FIRST
    legend('cosine')   % legend for the FIRST element
    

    如果我想为 cosine 和 -sine 添加一个图例:

    plot(t,c,t,m,t,s)  % cosine and -sine are first and second curves
    legend('cosine', '-sine')
    

    【讨论】:

      【解决方案5】:

      为了扩展塞巴斯蒂安的答案,我有一个特殊情况,我以两种格式之一绘制多条线(压缩或拉伸的桁架梁),并且能够在图例中绘制特定的绘图句柄,只要标签长度一样

      for ii=1:nBeams
          if X(ii)<0 %Bars with negative force are in compession
              h1=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
                  linspace(beamCord(ii,2),beamCord(ii,4)),'r:');
          elseif X(ii)>0 %Bars with positive force are in tension
              h2=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
                  linspace(beamCord(ii,2),beamCord(ii,4)),'b');
          end
      end
      
      legend([h1;h2],['Compression';'Tension    ']);
      

      在'Tension'后面添加了4个空格,以便字符数保持一致。

      【讨论】:

        【解决方案6】:

        快速的剧情破解:

        1. 删去你不想出现在图例中的所有内容
        2. 应用图例
        3. 粘贴

        【讨论】:

        • 剪切和粘贴是什么意思?你的意思是在legend 命令之后移动其他plot 命令?提供代码sn-p来演示。
        猜你喜欢
        • 1970-01-01
        • 2016-11-07
        • 2018-06-07
        • 2022-10-04
        • 2020-07-30
        • 2011-03-10
        • 2017-04-26
        • 2020-12-22
        • 1970-01-01
        相关资源
        最近更新 更多