MATLAB图形绘图:在绘图期间分配图例标签 [英] MATLAB graph plotting: assigning legend labels during plot

查看:101
本文介绍了MATLAB图形绘图:在绘图期间分配图例标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以典型的MATLAB散点图格式绘制数据.通常,在绘制多个数据集时,我将使用命令"hold on;",然后绘制每个数据,然后执行以下操作获得我的图例:

I am plotting data in a typical MATLAB scatterplot format. Ordinarily when plotting multiple datasets, I would use the command 'hold on;', and then plot each of the data, followed by this to get my legend:

legend('DataSet1', 'DataSet2') % etcetera

但是,我在相同轴上绘制的(多个)数据集不一定每次都相同.我正在同一轴上绘制多达六组不同的数据,并且可能会显示这些数据的任意组合(取决于用户选择显示的内容).显然,如果我想以传统方式设置图例,那将是很多其他事情.

However, the (multiple) datasets I am plotting on the same axes are not necessarily the same datasets each time. I am plotting up to six different sets of data on the same axes, and there could be any combination of these shown (depending on what the user chooses to display). Obviously that would be a lot of elseif's if I wanted to setup the legend the traditional way.

我真正想做的是为每个数据集分配一个名称​​(绘制时),这样以后我就可以调用所有显示数据的图例.

What I really would like to do is assign each DataSet a name as it is plotted so that afterwards I can just call up a legend of all the data being shown.

...或者,任何其他解决方案,任何人都可以想到..?

...Or, any other solution to this problem that anyone can think of..?

推荐答案

一种选择是利用'UserData'属性,如下所示:

One option is to take advantage of the 'UserData' property like so:

figure;
hold on
plot([0 1], [1 0], '-b', 'userdata', 'blue line')
plot([1 0], [1 0], '--r', 'userdata', 'red dashes')

% legend(get(get(gca, 'children'), 'userdata'))                      % wrong
legend(get(gca, 'children'), get(get(gca, 'children'), 'userdata'))  % correct

正如发问者所指出的那样,原始版本可能会出现问题.要解决此问题,请指定哪个手柄带有哪个标签(在固定版本中,标签的顺序正确).

As the questioner pointed out, the original version could get out of order. To fix this, specify which handle goes with which label (in the fixed version, it is in the correct order).

这篇关于MATLAB图形绘图:在绘图期间分配图例标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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