Remove legend key in matplotlib

拥有回忆 提交于 2019-12-02 06:51:16

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

and then add the text instead.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!