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

2024-06-17 10:14:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解决卡格尔的泰坦尼克号比赛

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

从这里,我需要看看有多少雄性/雌性幸存下来

我正在尝试以下方法:

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

但我得到了一个代表男女比例的曲线图:

enter image description here

知道如何使用seaborn创建堆叠酒吧吗

我需要绘制2个特征,每个特征有2个值


Tags: 方法datatrain代表特征sns男性女性
1条回答
网友
1楼 · 发布于 2024-06-17 10:14:50

我可能会尝试使用“分组条形图”。有趣的是,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")

相关问题 更多 >