删除matplotlib中的图例键 [英] Remove legend key in matplotlib
本文介绍了删除matplotlib中的图例键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示图例文本但没有键(默认显示的矩形框或线).
I want to show a legend text but without a key (the rectangular box or line that appears by default).
plt.hist(x, label = 'something')
我不希望图例某物"旁边的框.如何将其删除?
I don't want the box next to the legend "something". How to remove it?
推荐答案
首先,您可能决定完全不创建图例,而是在图的一角放置一些标签.
First of all, you may decide not to create a legend at all and instead put some label in to corner of the plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(size=160)
plt.hist(x)
plt.text(0.95,0.95, 'something', ha="right", va="top", transform=plt.gca().transAxes)
plt.show()
如果您已经创建了图例并想将其删除,则可以通过
If you already created the legend and want to remove it, you can do so by
plt.gca().get_legend().remove()
,然后添加文字.
如果这不是一个选项,则可以将图例句柄设置为不可见,如下所示:
If this is not an option, you may set the legend handle invisible like so:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(size=160)
plt.hist(x, label = 'something')
plt.legend()
leg = plt.gca().get_legend()
leg.legendHandles[0].set_visible(False)
plt.show()
这篇关于删除matplotlib中的图例键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文