hkB*_*sai 38 matlab plot legend
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', '');
Run Code Online (Sandbox Code Playgroud)

我的绘图中有几条曲线.我想只为其中一些人展示传奇.我该怎么做?
例如,如何在上面的绘图中仅显示余弦曲线的图例?当我调用legend()函数legend('', 'cosine');而不是添加空的第三个参数时,确实从图例中删除了第三个绿线.但这并没有解决我的问题,因为不需要的红线保持可见.
Mix*_*ryx 35
我不喜欢存储句柄值,当我的图中有很多图形时,它变得一团糟.因此我找到了另一个解决方
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
Run Code Online (Sandbox Code Playgroud)
这给了我与Eitan T的回答相同的图表.
应该注意的是,这也会影响其他matlab函数,例如cla只会删除图例中提到的图.在Matlab文档中搜索HandleVisibility以获取更多相关信息.
Eit*_*n T 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
Run Code Online (Sandbox Code Playgroud)
你应该得到这个情节:

让我们从您的变量开始并绘制它们:
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);
Run Code Online (Sandbox Code Playgroud)
有一个名为IconDisplayStyle的属性.它被埋得很深.您需要遵循的路径是:
Line - > Annotation - > LegendInformation - > IconDisplayStyle
设置IconDisplayStyle属性off将允许您跳过该行.举个例子,我要关掉hs传奇.
hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
Run Code Online (Sandbox Code Playgroud)
当然你可以这样做:
set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
Run Code Online (Sandbox Code Playgroud)
但我发现它更难理解.
现在,该legend功能将跳过hs.
用这个结束我的代码:
legend('cosine', 'repeat for this handle')
Run Code Online (Sandbox Code Playgroud)
会给你这个:

编辑:Jonas在评论中提出了一个很好的建议:设置DisplayNamehc 的属性如下:
set(hc, 'DisplayName', 'cosine');
legend(gca, 'show');
Run Code Online (Sandbox Code Playgroud)
会给你你需要的传奇.您将与您的行句柄关联'cosine'.因此,您可以使用'off'或'show'参数调用图例.
| 归档时间: |
|
| 查看次数: |
53444 次 |
| 最近记录: |