我想从我的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图例标题
发布评论