如何在绘图中仅为特定的曲线子集显示图例?

40
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');而不是添加空的第三个参数,确实从图例中删除了第三条绿线。但这并不能解决我的问题,因为不需要的红线仍然可见。

6个回答

39

我不喜欢存储句柄值,当我有很多图形时,这会变得混乱。因此,我找到了另一种解决方案。

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以获取更多相关信息。


2
我也使用了这个,因为我使用动态命名的曲线(在图例中隐藏标准差非常有效)。 - gaborous
2
如果想直接在 UI 中进行这些更改,最佳解决方案。 - G. Führ

27

只需将所需的图例句柄存储在变量中,然后将数组传递给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

你应该得到这个图表:

enter image description here


2
请注意,使用此方法后,一旦通过UI关闭图例并重新打开图例,所有线条都将重新出现在图例中。 - Jonas
1
PNG对于这种类型的图像更好。 - Mechanical snail
1
附注:这基本上是在http://www.mathworks.com/help/matlab/creating_plots/include-subset-of-objects-in-graph-legend.html推荐的内容。 - Carl Witthoft

6

让我们从您的变量开始,然后绘制它们:

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的属性。它被深深地埋藏了起来。你需要遵循以下路径:

Line -> Annotation -> 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')

将会给你这个:

enter image description here

编辑: Jonas在评论中提出了一个不错的建议: 像这样设置hc的DisplayName属性:

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

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


2
我建议设置线条句柄的 DisplayName 属性,而不是使用名称调用 legend,这样在 GUI 中切换图例开/关后结果将保持不变。 - Jonas

1
您可以更改曲线绘制的顺序,并将图例应用于第一条曲线:
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

如果我想为余弦和负正弦添加图例:
plot(t,c,t,m,t,s)  % cosine and -sine are first and second curves
legend('cosine', '-sine')

1
为了扩展Sebastian的答案,我有一个特殊情况,其中我绘制了两种格式(压缩或张力中的桁架梁)中的多条线,并能够在图例中绘制特定的绘图句柄,只要标签长度相同。
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个空格,以使字符数保持一致。


-2

快速内部修改:

  1. 剪切不想出现在图例中的内容
  2. 应用图例
  3. 粘贴

“剪切和粘贴”是什么意思?你是指将其他的 plot 命令移动到 legend 命令之后吗?请提供一个代码片段来演示。 - Cecilia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接