一个组合的图例条目可用于多个图 [英] One combined legend entry for multiple plots

查看:83
本文介绍了一个组合的图例条目可用于多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我想分别绘制同一数据的线和标记.

For some reason, I would like to plot the line and marker of the same data separately.

data1 = (1:1:10)';
data2 = (1:2:10);
figure(1);
plot(data1,data1,'or');
hold on;
plot(data2,data2,'-r');
legend('data');

但是,它将仅显示第一个情节的图例.而且Matlab似乎没有选择来操纵图例标记,颜色和线型的选项.

However it will only display the legend for the first plot. And Matlab seems not to have option to manipulate the legend marker, color and linestyle.

如何制作这样的图例?

谢谢!

推荐答案

您将需要绘制不可见的第三个图(几乎没有数据可以保持快速)来定义图例:

You will need to plot an invisible third plot (with almost no data to keep it fast) to define your legend:

data1 = (1:1:10)';
data2 = (1:2:10);
figure(1);
plot(data1,data1,'or'); hold on
plot(data2,data2,'-r'); hold on

%// legend plot
lp = plot(0,0,'-r','Marker','o','visible','off')
legend(lp,'data');

您需要将该无形图的句柄传递给legend命令,或者甚至可以将无形图放入图例中:

You need to pass the handle of that invisible plot to the legend command or you could even put the invisible plot into the legend:

legend(plot(0,0,'-r','Marker','o','visible','off'),'data');

如果需要更多,可以编写一些辅助功能

If you need that more often, you can write a little helper function

style = @(LineStyle, MarkerStyle) plot(0,0,LineStyle,'Marker',MarkerStyle,'visible','off')
legend(style('-r','o'),'data');

...,您可以使用'color''LineWidth'或任何您想要的内容进行自定义.

... which you can customize with 'color', 'LineWidth' or whatever you want.

它使您可以独立于实际数据创建具有多个条目的完全定制的图例:

It enables you to create fully customized legends with multiple entries independently from your actually data:

legend([style('-r','o'),style('-b','x'),style('-g','v')],{'1','2','3'});

这篇关于一个组合的图例条目可用于多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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