要将图例添加到matplotlib图例中,只需运行legend()。
如何从一个情节中移除一个传说?
(我最接近这一点的是运行legend([]),以便从数据中清空传说。但这在右上角留下了一个丑陋的白色矩形。)
要将图例添加到matplotlib图例中,只需运行legend()。
如何从一个情节中移除一个传说?
(我最接近这一点的是运行legend([]),以便从数据中清空传说。但这在右上角留下了一个丑陋的白色矩形。)
当前回答
如果你把pyplot称作PLT
frameon=False是删除图例周围的边框
“传递的信息是,图例中不应该有变量
import matplotlib.pyplot as plt
plt.legend('',frameon=False)
其他回答
你必须添加以下几行代码:
ax = gca()
ax.legend_ = None
draw()
Gca()返回当前坐标轴句柄,并具有传奇_属性
如果您使用seaborn,您可以使用参数图例。即使你在同一个数字上画了不止一次。使用一些df的例子
import seaborn as sns
# Will display legend
ax1 = sns.lineplot(x='cars', y='miles', hue='brand', data=df)
# No legend displayed
ax2 = sns.lineplot(x='cars', y='miles', hue='brand', data=df, legend=None)
你可以使用图例的set_visible方法:
ax.legend().set_visible(False)
draw()
这是基于一段时间前我在这里遇到的类似问题的答案
(谢谢你的回答,Jouni -我很抱歉我不能把问题标记为回答…也许有权威的人可以为我做这件事?)
你可以简单地这样做:
中心——axs [n]。传奇(loc =“左上”,ncol = 2, labelspacing = 0.01)
(我的4、5、6,7,8,9,10,11,12日,13日,14日,15日,16日,17日,18日19): 中心——axs[我].legend ([])
根据@naitsirhc提供的信息,我想找到官方的API文档。下面是我的发现和一些示例代码。
我创建了一个matplotlib。通过seaborn.scatterplot()实现坐标轴对象。 ax.get_legend()将返回matplotlib. leged . legend实例。 最后,调用.remove()函数从图中删除图例。
ax = sns.scatterplot(......)
_lg = ax.get_legend()
_lg.remove()
如果你检查matplotlib.legned.Legend API文档,你不会看到.remove()函数。
原因是matplotlib. leged . legend继承了matplotlib.artist.Artist。因此,当你调用ax.get_legend().remove()时,基本上会调用matplotlib.artist.Artist.remove()。
最后,您甚至可以将代码简化为两行。
ax = sns.scatterplot(......)
ax.get_legend().remove()