删除seaborn lineplot图例标题 [英] Remove seaborn lineplot legend title

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

问题描述

我想从我的sealine lineplot图例中删除标题.我尝试使用此答案无济于事:

I would like to remove the title from my seaborn lineplot legend. I tried using this answer to no avail:

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
fmri = sns.load_dataset("fmri")
fig, ax = plt.subplots()
g = sns.lineplot(x="timepoint", y="signal", hue="event", data=fmri, ax=ax)
ax.legend().set_title('')

如果我尝试将标题设置为None,我将得到相同的结果.有趣的是,将标题设置为其他名称似乎先于现有标题:

I get the same if I try to set the title to None. Interestingly, setting the title to something else seems to prepend to the existing title:

ax.legend().set_title('Something else')

seaborn似乎将标题视为隐藏的图例条目.我该如何解决?

It almost looks like seaborn is treating the title as a hidden legend entry. How can I resolve this?

推荐答案

重要:此答案与使用hue作为图例标题出现的情况有关.在所有其他情况下,问题本身已经包含摆脱标题的常用方法.

Important: This answer is about the case when a hue is used that appears as a legend title. In all other cases, the question itself already contains the usual way to get rid of a title.

确实,seaborn正在将图例标签误用作(subgroup-)标题.因此,想法可能是删除该标签,或将其替换为自定义文本.

Indeed, seaborn is misusing a legend label as a (subgroup-)title. Hence the idea can be to either remove this label, or replace it with custom text.

替换为自定义文本:

legend = ax.legend()
legend.texts[0].set_text("Whatever else")

移除标签:

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles[1:], labels=labels[1:])

删除标签后,您当然还可以设置另一个(真实)标题:

After having removed the label you may of course still set another (real) title:

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles[1:], labels=labels[1:], title="Whatever else")

这篇关于删除seaborn lineplot图例标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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