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

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

问题描述

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

解决方案

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

您应该得到以下情节:

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?

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.

解决方案

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

You should get this plot:

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

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