Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何在绘图中仅显示特定曲线子集的图例?_Matlab_Plot_Legend - Fatal编程技术网

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

Matlab 如何在绘图中仅显示特定曲线子集的图例?,matlab,plot,legend,Matlab,Plot,Legend,我的绘图中有几条曲线。我只想显示其中一些的图例。我该怎么做 例如,如何在上面的绘图中仅显示余弦曲线的图例?当我调用legend()函数时,它的作用是legend(''cosine')不添加空的第三个参数,而是从图例中删除第三条绿线。但这并不能解决我的问题,因为不需要的红线始终可见。只需将所需的图例句柄存储在变量中,并将数组传递给图例。在您的情况下,它将只有一个值,如下所示: t = 0 : 0.01 : 2 * pi; s = sin(t); c = cos(t); m = -sin(t);

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

例如,如何在上面的绘图中仅显示余弦曲线的图例?当我调用
legend()
函数时,它的作用是
legend(''cosine')
不添加空的第三个参数,而是从图例中删除第三条绿线。但这并不能解决我的问题,因为不需要的红线始终可见。

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

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', '');
你应该得到这个图:


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

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
有一个属性叫做。它埋得很深。您需要遵循的路径是:

线条->注释->传奇信息->图标显示样式

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

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);
当然,你可以这样做:

hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
legend('cosine', 'repeat for this handle')
但我发现这更难理解

现在,
legend
功能将跳过
hs

以以下内容结束我的代码:

set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
我会给你这个:

编辑:乔纳斯在评论中提出了一个很好的建议: 如下所示设置hc的
DisplayName
属性:

hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
legend('cosine', 'repeat for this handle')

将为您提供所需的传奇。您将把行句柄与
“余弦”
相关联。因此,您可以使用
'off'
'show'
参数调用图例。

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

set(hc, 'DisplayName', 'cosine');
legend(gca, 'show');
如果我想输入余弦和正弦的图例:

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')
这给了我与Eitan T的答案相同的图表


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

为了扩展Sebastian的答案,我有一个特殊的例子,我正在以两种格式之一(压缩或拉伸桁架梁)绘制几条线,并且只要标签长度相同,就能够在图例中绘制特定的绘图手柄

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
ii=1:nBeams的

如果X(ii)0%正力钢筋处于张力状态
h2=绘图(linspace(光束线(ii,1)、光束线(ii,3)),。。。
linspace(束线(ii,2),束线(ii,4)),'b');
结束
结束
图例([h1;h2],“压缩”;“张力”];
其中,在“张力”后面添加了4个空格,以便字符数保持一致。

快速绘图:

  • 剪切不希望出现在图例中的所有内容
  • 应用图例
  • 粘贴

  • 请注意,使用这种方法,一旦您关闭图例并通过UI将其重新打开,所有线条都将返回到图例中。PNG更适合这种图像。旁注:这基本上是我建议设置线条句柄的
    DisplayName
    属性的建议,而不是使用名称调用
    legend
    ,这样在GUI中关闭/打开图例后,结果将是相同的。谢谢@Jonas。更新了我的答案。也使用了这个,因为我使用了动态命名的曲线(非常适合绘制标准偏差,但将其隐藏在图例中)。如果要直接在UI中进行此更改,最好的解决方案。剪切和粘贴是什么意思?您的意思是将其他
    绘图
    命令移到
    图例
    命令之后吗?提供一个代码片段进行演示。