如何防止图例在R2017a及更高版本中更新? [英] How to prevent the legend from updating in R2017a and newer?

查看:82
本文介绍了如何防止图例在R2017a及更高版本中更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自MATLAB R2017a起,将图添加到轴时,图例会自动更新.以前,可以执行以下操作:

Since MATLAB R2017a, figure legends update automatically when adding a plot to axes. Previously, one could do this:

data = randn(100,4);
plot(data)
legend('line1','line2','line3','line4')
hold on
plot([1,100],[0,0],'k-')

绘制带有图例的四条数据线,然后为y = 0添加一条黑线.但是,从R2017a开始,这导致将黑色线条添加到图例中,名称为"data1".

to plot four data lines with a legend, and then add a black line for y=0. However, since R2017a, this leads to the black line being added to the legend, with the name "data1".

如何防止将此行添加到图例中,以使代码的行为像在旧版本的MATLAB中一样?

How do I prevent this line from being added to the legend, so that the code behaves like it did in older versions of MATLAB?

到目前为止,我在Stack Overflow上找到的唯一解决方案是在添加图例后将其删除.语法不太漂亮:

The only solution I have found so far on Stack Overflow is to remove the legend item after it has been added. The syntax is not pretty:

h = plot([1,100],[0,0],'k-'); % keep a handle to the added line
set(get(get(h,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');

推荐答案

MATLAB R2017a的发行说明

The release notes for MATLAB R2017a mention this change, and provide 4 different ways of handling the situation. These two methods are easiest to put into existing code:

1 :在添加黑线之前,请关闭图例的自动更新.这可以在创建时完成:

1: Turn off auto updating for the legend before adding the black line. This can be done at creation time:

legend({'line1','line2','line3','line4'}, 'AutoUpdate','off')

或之后:

h = findobj(gcf,'type','legend');
set(h, 'AutoUpdate','off')

您还可以更改所有将来图例的默认值:

You can also change the default for all future legends:

set(groot,'defaultLegendAutoUpdate','off')


2::关闭不需要添加到图例中的黑线的手柄可见性:


2: Turn off handle visibility for the black line that you don't want added to the legend:

plot([1,100],[0,0],'k-', 'HandleVisibility','off')


IconDisplayStyle方法也在此处显示.但是,它们使用点表示法,这使语法有点漂亮:


The IconDisplayStyle method is also shown here. However, they use the dot notation, which makes the syntax is a bit prettier:

h = plot([1,100],[0,0],'k-'); % keep a handle to the added line
h.Annotation.LegendInformation.IconDisplayStyle = 'off';

这篇关于如何防止图例在R2017a及更高版本中更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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