删除 Seaborn 条形图图例标题
Remove Seaborn barplot legend title
我使用 seaborn 绘制分组条形图,如 https://seaborn.pydata.org/examples/factorplot_bars.html
给我:
https://seaborn.pydata.org/_images/factorplot_bars.png
我想删除图例上的标题(性别)。
我怎样才能做到这一点?
这可能是一个 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()
示例结果:
一种不太老套的方法是使用 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()
结果
如果您希望图例显示在绘图轴之外,这是 factorplot
的默认设置,您可以使用 FacetGrid.add_legend
(factorplot
returns a FacetGrid
实例)。其他方法允许您一次调整FacetGrid
中每个轴的标签
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
g = sns.factorplot(x="class", y="survived", hue="sex", data=titanic,
size=6, kind="bar", palette="muted", legend=False)
(g.despine(left=True)
.set_ylabels('survival probability')
.add_legend(title='Whatever you want')
)
您可以删除图例标题:
plt.gca().legend().set_title('')
我使用 seaborn 绘制分组条形图,如 https://seaborn.pydata.org/examples/factorplot_bars.html
给我: https://seaborn.pydata.org/_images/factorplot_bars.png
我想删除图例上的标题(性别)。
我怎样才能做到这一点?
这可能是一个 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()
示例结果:
一种不太老套的方法是使用 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()
结果
如果您希望图例显示在绘图轴之外,这是 factorplot
的默认设置,您可以使用 FacetGrid.add_legend
(factorplot
returns a FacetGrid
实例)。其他方法允许您一次调整FacetGrid
中每个轴的标签
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
g = sns.factorplot(x="class", y="survived", hue="sex", data=titanic,
size=6, kind="bar", palette="muted", legend=False)
(g.despine(left=True)
.set_ylabels('survival probability')
.add_legend(title='Whatever you want')
)
您可以删除图例标题:
plt.gca().legend().set_title('')