Seaborn countplot with normalized y axis per group

后端 未结 5 810
执笔经年
执笔经年 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:26

    You can provide estimators for the height of the bar (along y axis) in a seaborn countplot by using the estimator keyword.

    ax = sns.barplot(x="x", y="x", data=df, estimator=lambda x: len(x) / len(df) * 100)
    

    The above code snippet is from https://github.com/mwaskom/seaborn/issues/1027

    They have a whole discussion about how to provide percentages in a countplot. This answer is based off the same thread linked above.

    In the context of your specific problem, you can probably do something like this:

    ax = sb.barplot(x='occupation', y='some_numeric_column', data=raw_data, estimator=lambda x: len(x) / len(raw_data) * 100, hue='income')
    ax.set(ylabel="Percent")
    

    The above code worked for me (on a different dataset with different attributes). Note that you need to put in some numeric column for y else, it gives an error: "ValueError: Neither the x nor y variable appears to be numeric."

提交回复
热议问题