2017-06-17 57 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