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

関連する問題