Seaborn多个木槌 [英] Seaborn multiple barplots
本文介绍了Seaborn多个木槌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的熊猫数据框:
I have a pandas dataframe that looks like this:
class men woman children
0 first 0.91468 0.667971 0.660562
1 second 0.30012 0.329380 0.882608
2 third 0.11899 0.189747 0.121259
我将如何使用seaborn来创建一个像这样的情节?我必须以某种方式重新排列我的数据吗?
How would I create a plot using seaborn that looks like this? Do I have to rearrange my data in some way?
(来源:stanford.edu上的 mwaskom )
(source: mwaskom at stanford.edu)
推荐答案
是的,您需要重塑DataFrame:
Yes you need to reshape the DataFrame:
df = pd.melt(df, id_vars="class", var_name="sex", value_name="survival rate")
df
Out:
class sex survival rate
0 first men 0.914680
1 second men 0.300120
2 third men 0.118990
3 first woman 0.667971
4 second woman 0.329380
5 third woman 0.189747
6 first children 0.660562
7 second children 0.882608
8 third children 0.121259
现在,您可以使用factorplot(v0.8.1或更早版本):
Now, you can use factorplot (v0.8.1 or earlier):
sns.factorplot(x='class', y='survival rate', hue='sex', data=df, kind='bar')
对于0.9.0或更高版本,如Matthew在评论中所述,您需要使用重命名的版本catplot
.
For versions 0.9.0 or later, as Matthew noted in the comments, you need to use the renamed version, catplot
.
sns.catplot(x='class', y='survival rate', hue='sex', data=df, kind='bar')
这篇关于Seaborn多个木槌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文