首页 > 解决方案 > 频率 Seaborn 计数图

问题描述

这是一些基本的代码示例:

import seaborn as sns
titanic = sns.load_dataset("titanic")
ax = sns.countplot(x="class", hue="who", data=titanic)

在此处输入图像描述

对于变量“类”的每个标签,我想要的只是比方说人的频率历史。

例如,对于 "class"="First",我们应该让男性的比例等于 110/(110+90+5) 。

seaborn可以吗?谢谢

标签: pythonpandasmatplotlibseaborn

解决方案


您可能希望将只有男性的计数除以由"class". (注意这里 seaborn 只用来加载数据集。)

import seaborn as sns
titanic = sns.load_dataset("titanic")

df1 = titanic.groupby("class").size()
df2 = titanic[titanic["who"] == "man"].groupby("class").size()

(df2/df1).plot.bar(title="Proportion of men on titanic", color="C0")

在此处输入图像描述


推荐阅读