2017-06-17 101 views
1

我想显示图例文本但没有键(默认情况下出现的矩形框或线)。删除matplotlib中的图例键

plt.hist(x, label = 'something') 

enter image description here

我不想旁边的传说 “东西” 的复选框。如何删除它?

回答

2

首先,您可能决定不创建图例,而是将一些标签放在图的一角。

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() 

enter image description here

如果您已经创建了传奇,并希望删除它,你可以通过

plt.gca().get_legend().remove() 

这样做,然后添加文字。

如果这不是一个选项,你可以通过设置传奇隐形处理,像这样:

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() 

enter image description here

+0

我知道的文本,但认为这可能在matplotlib来完成。谢谢! – Peaceful