简体   繁体   English

修改seaborn line relplot图例标题

[英]Modify seaborn line relplot legend title

First off, I am a newbie with both Python and seaborn, but after spending some time with this matter I believe my question has not been addressed elsewhere.首先,我是 Python 和 seaborn 的新手,但在这件事上花了一些时间后,我相信我的问题没有在其他地方得到解决。 If so, I sincerely apologize.如果是这样,我真诚地道歉。

I want to use the seaborn relplot() function with kind='line' to plot data from a dataframe as several lines of different colors.我想使用带有kind='line'的 seaborn relplot()函数将数据relplot()的数据绘制为多条不同颜色的行。 The data corresponding to the lines are separated categorically by values in a column ( cat , say) and I use hue (in this case, hue='cat' ).与行相对应的数据由列中的值( cat ,例如)分类分隔,我使用hue (在这种情况下, hue='cat' )。 Here is a simple example:这是一个简单的例子:

import pandas as pd
import seaborn as sns
df = pd.DataFrame({'x': [0, 1, 3, 4, 7, 1, 2, 5, 6],
                   'y': [2, 4, 3, 6, 5, 7, 10, 9, 7],
                   'cat': [0, 0, 0, 0, 0, 1, 1, 1, 1]})
g = sns.relplot(x='x', y='y', hue='cat', kind='line', data=df);

It produces the following graph:它产生以下图形:

示例图

How do I modify the legend title without changing the name of the column cat ?如何修改图例标题而不更改列cat的名称? I believe my problem is that the legend title is actually implemented as a legend item, so what I want to do is remove that item and then add a proper legend title (which I can do using g._legend.set_title('New title') ), but I do not know how to accomplish this.我相信我的问题是图例标题实际上是作为图例项目实现的,所以我想要做的是删除该项目,然后添加适当的图例标题(我可以使用g._legend.set_title('New title') ),但我不知道如何做到这一点。

This question differs from Remove seaborn lineplot legend title in that relplot() with kind='line' produces a FacetGrid figure and attaches the legend to that one.这个问题与Remove seaborn lineplot legend title 的不同之处在于relplot() with kind='line'生成一个FacetGrid图并将图例附加到那个图上。 This means I cannot access the legend content as g.ax.legend().texts but must use g._legend , in which case I am lost.这意味着我无法以g.ax.legend().texts访问图例内容,但必须使用g._legend ,在这种情况下我迷路了。

One option would be to set the first legend entry to an empty string using g._legend.texts[0].set_text("") , then use g._legend.set_title('New title')一种选择是使用g._legend.texts[0].set_text("")将第一个图例条目设置为空字符串,然后使用g._legend.set_title('New title')

import seaborn as sns
df = pd.DataFrame({'x': [0, 1, 3, 4, 7, 1, 2, 5, 6],
                   'y': [2, 4, 3, 6, 5, 7, 10, 9, 7],
                   'cat': [0, 0, 0, 0, 0, 1, 1, 1, 1]})
g = sns.relplot(x='x', y='y', hue='cat', kind='line', data=df)

g._legend.texts[0].set_text("")
g._legend.set_title("New title")
g._legend._legend_box.sep = -5  # move title down slightly

plt.show()

在此处输入图片说明

You can also change the horizontal alignment using:您还可以使用以下方法更改水平对齐方式:

g._legend._legend_box.align = "right"  # or left, or center

Another option would be to draw the legend yourself:另一种选择是自己绘制图例:

g = sns.relplot(x='x', y='y', hue='cat', kind='line', data=df, legend=False)
ax =g.axes[0][0]

for i, line in enumerate(ax.lines):
    line.set_label(i)

leg = ax.legend()
leg.set_title("New title")

An alternative to setting the first legend entry to an empty string is shown below (suggested by ImportanceOfBeingErnest in the comments):下面显示了将第一个图例条目设置为空字符串的替代方法(由评论中的 ImportanceOfBeingErnest 建议):

c = g._legend.get_children()[0].get_children()[1].get_children()[0]
c._children = c._children[1:]

For me, g._legend.texts[0].set_text("") removed the label of the first element in the legend, not the title.对我来说, g._legend.texts[0].set_text("")删除了图例中第一个元素的标签,而不是标题。 Maybe seaborn 0.11.1 defines g._legend.texts[0] differently.也许 seaborn 0.11.1 对g._legend.texts[0]定义不同。

Therefore I simply used因此我简单地使用了

g._legend.set_title("")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM