Seaborn countplot with normalized y axis per group

后端 未结 5 808
执笔经年
执笔经年 2020-12-12 16:28

I was wondering if it is possible to create a Seaborn count plot, but instead of actual counts on the y-axis, show the relative frequency (percentage) within its group (as s

5条回答
  •  离开以前
    2020-12-12 17:28

    I might be confused. The difference between your output and the output of

    occupation_counts = (df.groupby(['income'])['occupation']
                         .value_counts(normalize=True)
                         .rename('percentage')
                         .mul(100)
                         .reset_index()
                         .sort_values('occupation'))
    p = sns.barplot(x="occupation", y="percentage", hue="income", data=occupation_counts)
    _ = plt.setp(p.get_xticklabels(), rotation=90)  # Rotate labels
    

    is, it seems to me, only the order of the columns.

    And you seem to care about that, since you pass sort=False. But then, in your code the order is determined uniquely by chance (and the order in which the dictionary is iterated even changes from run to run with Python 3.5).

提交回复
热议问题