Python和Seaborn如何使用barplot绘制两个分类特征

问题描述 投票:0回答:1

我正在尝试解决kaggle的泰坦尼克号竞争。

我需要生成一个绘图,其中X代表以男性和女性为值的Sex。和Y作为两个变量0和1。

由此,我需要查看有多少男性/女性幸存。

我正在尝试以下操作:

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

但是我得到的图代表了每个男性和女性的百分比:

enter image description here

任何想法如何使用Seaborn来创建堆叠式条形图?

我需要绘制2个要素,每个要素都有2个值。

python matplotlib machine-learning seaborn
1个回答
0
投票

我可能会尝试使用“成组的条形图”。有趣的是,seaborn的画廊页面上有一个很好的example ...以泰坦尼克号数据为例:

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")
© www.soinside.com 2019 - 2024. All rights reserved.