【问题标题】:Python and Seaborn how to plot two categorical features using barplotPython 和 Seaborn 如何使用 barplot 绘制两个分类特征
【发布时间】:2020-05-30 22:35:01
【问题描述】:
我正在尝试解决 kaggle 的巨大竞争。
我需要生成一个图,其中 X 代表 Sex 以男性和女性为值。和 Y 作为两个变量 0 和 1。
由此,我需要看看有多少男性/女性幸存下来。
我正在尝试以下方法:
sns.barplot(x='Sex', y='Survived', data=train)
但我得到了一个代表每个男性和女性百分比的图:
知道如何使用 seaborn 创建堆叠条吗?
我需要绘制 2 个特征,每个特征都有 2 个值。
【问题讨论】:
标签:
python
matplotlib
machine-learning
seaborn
【解决方案1】:
我可能会尝试使用“分组条形图”。有趣的是,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")