如何清除图例中的matplotlib标签? [英] How to clear matplotlib labels in legend?

查看:109
本文介绍了如何清除图例中的matplotlib标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以清除图例中的matplotlib标签?

Is there a way to clear matplotlib labels inside a graph's legend? This post explains how to remove the legend itself, but the labels themselves still remain, and appear again if you plot a new figure. I tried the following code, but it does not work:

handles, labels = ax.get_legend_handles_labels()
labels = []

EDIT: Here is an example

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)

Output: ["a"]

解决方案

Use set_visible() method:

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标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆