Matlab:更改图例中条目的顺序 [英] Matlab: change order of entries in Figure legend

查看:3539
本文介绍了Matlab:更改图例中条目的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Figure文件,我想在其中更改条目的顺序(例如,将第一个条目作为第三个条目).我很早以前保存了这个Figure.fig,所以不确定是否可以恢复原始代码.

I have a Figure file where I would like to change the order of the entries (e.g., put the first entry as third one). I saved this Figure.fig long time ago so I am not sure if I can recover the original code.

在这里,我向您展示我的阴谋:

Here I show you my plot:

我希望图例元素按降序排列(如图所示),但是由于一个错误,我的第二个输入是指错误的绘图(显示为"25年",但该绘图实际上是指最低的绘图)趋势,对应于"9年"趋势.

I want the legend elements to be in a decreasing order ( as in the picture) but due to a mistake my second entry is referring to the wrong plot (it says "25 years" but the plot is actually referred to the lowest trend, corresponding to the "9 years" trend).

我可以直接从Matlab中图形的属性编辑器切换图例中条目的顺序吗?如果是,如何(我没有看到任何"Order"属性或类似属性)?否则,是否有其他简单方法可以切换图例中条目的顺序?

Can I switch the order of the entries in the Legend directly from the Properties Editor of the Figure in Matlab? If yes, how (I did not see any "Order" property or similar)? Otherwise is there any other simple approach to switch the order of the entries in the Legend?

推荐答案

如果您的图形是在R2014b或更高版本中生成的,则可以使用未记录的'PlotChildren'属性来操作图例条目的顺序,而无需新的 legend 呼叫.

If your figure was generated in R2014b or newer you can utilize the undocumented 'PlotChildren' property to manipulate the order of the legend entries without requiring a new legend call.

例如:

x = 1:10;
y1 = x;
y2 = 2*x;
y3 = 3*x;
y4 = x.^2;

plot(x, y1, x, y2, x, y3, x, y4);
lh = legend('y = x', 'y = 2*x', 'y = 3*x', 'y = x.^2');

产生:

然后您可以进行以下操作:

Which you can then manipulate:

neworder = [3, 1, 4, 2];
lh.PlotChildren = lh.PlotChildren(neworder);

制作:

如果您没有legend对象的句柄,则它是 figure 对象,其中包含 axes 绘制数据的对象.您可以使用以下 findobj其中之一来找到legend对象的句柄方法:

If you don't have the handle to the legend object, it is a child of the figure object containing the axes object your data is plotted on. You can find the handle to your legend object using one of the following findobj approaches:

% Handle to figure object known
lg = findobj(figureobj, 'Type', 'legend');

% Handle to figure object unknown
lh = findobj(gcf, 'Type', 'legend');

请注意, gcf 通常 >将句柄返回到用户单击的最后一个数字,但这并非总是如此.

Note that gcf generally returns the handle to the last figure that the user clicked on, but this is not necessarily always the case.

自我促销编辑:此方法包含在一组维护的传奇操作工具中GitHub (由StackOverflow MATLAB社区提供).

Self promotion edit: This method is included in a set of legend manipulation tools maintained on GitHub by the StackOverflow MATLAB community.

这篇关于Matlab:更改图例中条目的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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