删除Seaborn barplot图例标题

use*_*531 16 python matplotlib bar-chart seaborn

我使用seaborn绘制分组条形图,如https://seaborn.pydata.org/examples/factorplot_bars.html

给我:https: //seaborn.pydata.org/_images/factorplot_bars.png

传说中有一个标题(性别),我想删除.

我怎么能实现这一目标?

1''*_*1'' 14

您可以使用以下命令删除图例标题:

plt.gca().legend().set_title('')

  • 这应该是答案。它可以工作并且需要最少的假设和代码。 (2认同)

ber*_*nie 11

这可能是一个hacky解决方案,但它的工作原理:如果你告诉Seaborn在绘图时将其关闭,然后将其添加回来,它就没有传说标题:

g = sns.factorplot(x='Age Group',y='ED',hue='Became Member',col='Coverage Type',
                   col_wrap=3,data=gdf,kind='bar',ci=None,legend=False,palette='muted')
#                                                         ^^^^^^^^^^^^
plt.suptitle('ED Visit Rate per 1,000 Members per Year',size=16)
plt.legend(loc='best')
plt.subplots_adjust(top=.925)
plt.show()
Run Code Online (Sandbox Code Playgroud)

示例结果:

在此输入图像描述


dil*_*lon 10

一种不太常见的方法是使用matplotlib的面向对象的接口.通过获得轴的控制,可以更容易地定制绘图.

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

# Load the example Titanic dataset
titanic = sns.load_dataset("titanic")

# Draw a nested barplot to show survival for class and sex
fig, ax = plt.subplots()
g = sns.factorplot(x="class", y="survived", hue="sex", data=titanic,
                   size=6, kind="bar", palette="muted", ax=ax)
sns.despine(ax=ax, left=True)
ax.set_ylabel("survival probability")
l = ax.legend()
l.set_title('Whatever you want')
fig.show()
Run Code Online (Sandbox Code Playgroud)

结果是 resulting_plot

  • 不幸的是,这对seaborn.lineplot(v0.9.0)不起作用. (3认同)