频率Seaborn计数图

时间:2019-01-22 13:39:38

标签: python pandas matplotlib seaborn

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

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

enter image description here

对于变量“ class”的每个标签,我想拥有的只是人类的出现频率。

例如,对于“ class” =“ First”,我们应该有等于110 /(110 + 90 + 5)的比例的人。

seaborn可能吗?谢谢

1 个答案:

答案 0 :(得分:0)

您可能希望将仅男性的人数除以"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")

enter image description here

相关问题