如何在绘图中仅显示特定曲线子集的图例? [英] How to show legend for only a specific subset of curves in the plotting?

查看:20
本文介绍了如何在绘图中仅显示特定曲线子集的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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', '');

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

There are several curves in my plotting. I want to display legend for only some of them. How do I do it?

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

For example, how do I make only the legend for the cosine curve visible in the plotting above? When I call the legend() functions as legend('', 'cosine'); instead of adding the empty third parameter, indeed the third green line is removed from the legend. But that doesn't solve my problem, because the undesired red line stays visible.

推荐答案

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

Just store the desired legend handles in a variable and pass the array to legend. In your case, it would only be one value, like so:

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

你应该得到这个情节:

这篇关于如何在绘图中仅显示特定曲线子集的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆