如何清除图例中的 matplotlib 标签?
How to clear matplotlib labels in legend?
有没有办法清除图表图例中的 matplotlib 标签? This post 解释了如何删除图例本身,但标签本身仍然存在,如果您绘制新图形,它们会再次出现。我尝试了以下代码,但它不起作用:
handles, labels = ax.get_legend_handles_labels()
labels = []
编辑:这是一个例子
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
ax.scatter([1,2,3], [4,5,6], label = "a")
legend = ax.legend()
plt.show()
legend.remove()
handles, labels = ax.get_legend_handles_labels()
print(labels)
输出:["a"]
使用set_visible()
方法:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
ax.scatter([1,2,3], [4,5,6], label = "a")
legend = ax.legend()
for text in legend.texts:
if (text.get_text() == 'a'): text.set_text('b') # change label text
text.set_visible(False) # disable label
plt.show()
有没有办法清除图表图例中的 matplotlib 标签? This post 解释了如何删除图例本身,但标签本身仍然存在,如果您绘制新图形,它们会再次出现。我尝试了以下代码,但它不起作用:
handles, labels = ax.get_legend_handles_labels()
labels = []
编辑:这是一个例子
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
ax.scatter([1,2,3], [4,5,6], label = "a")
legend = ax.legend()
plt.show()
legend.remove()
handles, labels = ax.get_legend_handles_labels()
print(labels)
输出:["a"]
使用set_visible()
方法:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
ax.scatter([1,2,3], [4,5,6], label = "a")
legend = ax.legend()
for text in legend.texts:
if (text.get_text() == 'a'): text.set_text('b') # change label text
text.set_visible(False) # disable label
plt.show()