行图例 [英] legend for group of lines
问题描述
我喜欢在同一图中绘制两组线.每组有两条相同颜色的线,我必须按照一组的顺序绘制它们.我尝试仅对组显示图例,而不对行显示图例.我怎样才能做到这一点?这是我错误代码的简化:
I like to plot two groups of lines in the same plot. Each group has two lines with same color and I have to draw them in the order of one group after another group. I try to show legend for groups only not for lines. How can I do this? Here is a simplification of my wrong code:
plot(x1, y1, color1); hold on;
plot(x2, y2, color1); hold on;
plot(x3, y3, color2); hold on;
plot(x4, y4, color2); hold on;
legend({color1, color2})
谢谢!
更新:
一个新问题,是否有任何方法可以在每行之后编写图例,而不会覆盖前一个图例,而是将其追加?即类似于保留",但适用于图例.
A new question, is there any way to write legend after each line and without overwriting the previous legend but append to it? i.e. something similar to "hold on" but applied to legend.
推荐答案
有几种方法可以做到这一点.最简单的方法是获取每个组的第一条绘制线的手柄并将其作为第一个参数传递给
There are a few ways you can do this. The easiest way is to get the handle for the first plotted line of each group and pass that as the first argument to LEGEND:
h1 = plot(x1, y1, color1);
hold on;
plot(x2, y2, color1);
h2 = plot(x3, y3, color2);
plot(x4, y4, color2);
legend([h1 h2],{'label1', 'label2'});
这篇关于行图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!