简体   繁体   English

matplotlib取消轴对象上的图例

[英]matplotlib cancel legend on axes object

I am using an external module which automatically adds a legend to the plot. 我正在使用外部模块,它会自动为图表添加图例。 I would like to know if there is a way of turning off the legend, something like ax.set_legend(False). 我想知道是否有一种关闭传说的方法,比如ax.set_legend(False)。

I could fix it by hacking the module but I would rather not do that. 我可以通过黑客攻击模块来修复它,但我宁愿不这样做。

example: 例:

 f = plt.figure()
 ax = f.add_subplot(111)

 externalfunction(ax)

 # in the function ax.legend() has been called
 # would like to turn off the legend here

 plt.show()

Update: 更新:

I have raised a github issue for this https://github.com/matplotlib/matplotlib/issues/2792 我为此https://github.com/matplotlib/matplotlib/issues/2792提出了一个github问题

你需要改变你的传奇的可见性,试试这个: ax.legend().set_visible(False)

This can also be accomplished by setting the legend_ attribute of the axis to None . 这也可以通过将轴的legend_属性设置为None Note the trailing underscore. 请注意尾随下划线。 Eg 例如

x, y = np.random.randn(2, 30)
ax = plt.gca()
ax.plot(x, y, label="data")
ax.legend()
ax.legend_ = None

It sounds like future matplotlib versions will have a more officially-sanctioned method for removing the axis, but this should work in the meantime/if stuck on an older version. 听起来未来的matplotlib版本将有一个更加官方认可的方法来移除轴,但这应该在此期间/如果卡在旧版本上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM