删除 matplotlib 图上的图例
要向 matplotlib 绘图添加图例,只需运行 legend()
即可。
如何从图中删除图例?
(我最接近的方法是运行 legend([])
来清空数据中的图例。但这会在右上角留下一个丑陋的白色矩形。)
To add a legend to a matplotlib plot, one simply runs legend()
.
How to remove a legend from a plot?
(The closest I came to this is to run legend([])
in order to empty the legend from data. But that leaves an ugly white rectangle in the upper right corner.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
自 matplotlib
v1.4.0rc4
,一个remove
方法已添加到图例对象中。用法:
或者
请参阅此处了解引入此功能的提交。
As of matplotlib
v1.4.0rc4
, aremove
method has been added to the legend object.Usage:
or
See here for the commit where this was introduced.
如果要绘制 Pandas 数据框并删除图例,请将 legend=None 作为参数添加到绘图命令中。
If you want to plot a Pandas dataframe and want to remove the legend, add legend=None as parameter to the plot command.
您可以使用图例的
set_visible
方法:这是基于我为回答我不久前遇到的类似问题而提供的答案此处
(感谢 Jouni 的回答 - 很抱歉,我无法将问题标记为已回答......也许有权限的人可以这样做为我?)
You could use the legend's
set_visible
method:This is based on a answer provided to me in response to a similar question I had some time ago here
(Thanks for that answer Jouni - I'm sorry I was unable to mark the question as answered... perhaps someone who has the authority can do so for me?)
如果您将
pyplot
调用为plt
frameon=False
是为了删除图例周围的边框,并且 '' 正在传递不应有任何变量的信息传说中
if you call
pyplot
asplt
frameon=False
is to remove the border around the legendand '' is passing the information that no variable should be in the legend
您必须添加以下代码行:
gca() 返回当前轴句柄,并具有该属性 legend_
you have to add the following lines of code:
gca() returns the current axes handle, and has that property legend_
根据@naitsirhc的信息,我想找到官方的API文档。这是我的发现和一些示例代码。
seaborn 创建了一个
。matplotlib.Axes
对象。散点图().remove()
函数从图中删除图例。如果您检查
matplotlib.legend.Legend
API 文档中,您不会看到
.remove()
函数。原因是
matplotlib.legend.Legend
继承了matplotlib.artist.Artist
。因此,当您调用 ax.get_legend().remove() 时,基本上会调用matplotlib.artist.Artist.remove()
。最后,您甚至可以将代码简化为两行。
According to the information from @naitsirhc, I wanted to find the official API documentation. Here are my finding and some sample code.
matplotlib.Axes
object byseaborn.scatterplot()
.ax.get_legend()
will return amatplotlib.legend.Legend
instance..remove()
function to remove the legend from your plot.If you check the
matplotlib.legend.Legend
API document, you won't see the.remove()
function.The reason is that the
matplotlib.legend.Legend
inherited thematplotlib.artist.Artist
. Therefore, when you callax.get_legend().remove()
that basically callmatplotlib.artist.Artist.remove()
.In the end, you could even simplify the code into two lines.
我通过将其添加到图形而不是轴(matplotlib 2.2.2)来制作图例。要删除它,我将图窗的
legends
属性设置为空列表:I made a legend by adding it to the figure, not to an axis (matplotlib 2.2.2). To remove it, I set the
legends
attribute of the figure to an empty list:如果您不使用无花果和斧头图对象,您可以这样做:
If you are not using fig and ax plot objects you can do it like so:
以下是使用
matplotlib
和seaborn
处理子图的图例删除和操作的更复杂示例:从 seaborn 中获取由() 并按照 @naitsirhc。下面的示例还展示了如何将图例放在一边,以及如何处理子图的上下文。
Axes
创建的Axes
对象code>sns.Here is a more complex example of legend removal and manipulation with
matplotlib
andseaborn
dealing with subplots:From seaborn, get the
Axes
object created bysns.<some_plot>()
and doax.get_legend().remove()
as indicated by @naitsirhc. The following example also shows how to put the legend aside, and how to deal in a context of subplots.如果您使用的是seaborn,则可以使用参数
legend
。即使您在同一个图中多次绘制。一些 df 的示例If you are using seaborn you can use the parameter
legend
. Even if you are ploting more than once in the same figure. Example with some df你可以简单地这样做:
axs[n].legend(loc='upper left',ncol=2,labelspacing=0.01)
for i in [4,5,6,7,8 ,9,10,11,12,13,14,15,16,17,18,19]:
axs[i].legend([])
you could simply do:
axs[n].legend(loc='upper left',ncol=2,labelspacing=0.01)
for i in [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]:
axs[i].legend([])