简体   繁体   English

Python 和 Seaborn 如何使用条形图绘制两个分类特征

[英]Python and Seaborn how to plot two categorical features using barplot

I am trying to solve kaggle's titanic competition.我正在尝试解决 kaggle 的巨大竞争。

I need to generate a plot where X is representing Sex having male and female as values.我需要生成一个图,其中 X 代表以男性和女性为值的Sex And Y as two variables 0 and 1.和 Y 作为两个变量 0 和 1。

From this, I need to see how many males/females survived.由此,我需要看看有多少男性/女性幸存下来。

I am trying the following:我正在尝试以下操作:

sns.barplot(x='Sex', y='Survived', data=train)

But I am getting a plot representing percentage of each male and female:但是我得到了一个代表每个男性和女性百分比的图:

在此处输入图片说明

Any idea how to create stacked bar using seaborn?知道如何使用 seaborn 创建堆叠条吗?

I need to plot 2 features, each of them having 2 values.我需要绘制 2 个特征,每个特征都有 2 个值。

I would probably try with "Grouped barplots".我可能会尝试使用“分组条形图”。 Interestingly, the seaborn's gallery page has a nice example about it... with titanic data as an example:有趣的是,seaborn 的画廊页面有一个很好的例子......以泰坦尼克号数据为例:

import seaborn as sns
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.catplot(x="class", y="survived", hue="sex", data=titanic,
                height=6, kind="bar", palette="muted")
g.despine(left=True)
g.set_ylabels("survival probability")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM