Python 删除 Seaborn 条形图图例标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43151440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Seaborn barplot legend title
提问by cppgnlearner
I use seaborn to plot a grouped bar plot as in https://seaborn.pydata.org/examples/factorplot_bars.html
我使用 seaborn 绘制分组条形图,如https://seaborn.pydata.org/examples/factorplot_bars.html
Giving me: https://seaborn.pydata.org/_images/factorplot_bars.png
给我:https: //seaborn.pydata.org/_images/factorplot_bars.png
there is a title (sex) on the legend which I would like to remove.
我想删除图例上的标题(性别)。
How could I achieve that?
我怎么能做到这一点?
采纳答案by mechanical_meat
This may be a hacky solution but it works: if you tell Seaborn to leave it off at the time of plotting and then add it back it doesn't have the legend title:
这可能是一个 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()
Example result:
结果示例:
回答by dillon
A less hacky way is to use the object oriented interface of matplotlib. By gaining control of the axes, it will make it a lot easier to customize the plot.
一种不那么笨拙的方法是使用 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()
回答by 1''
You can remove the legend title with:
您可以使用以下命令删除图例标题:
plt.gca().legend().set_title('')
plt.gca().legend().set_title('')
回答by fredcallaway
If you want the legend to be shown outside of the plot axis, as is default for factorplot
, you can use FacetGrid.add_legend
(factorplot
returns a FacetGrid
instance). Other methods allow you to adjust the labels of every axis in the FacetGrid
at once
如果您希望图例显示在绘图轴之外,这是 的默认设置factorplot
,您可以使用FacetGrid.add_legend
(factorplot
返回一个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')
)