Horizontal barplot in Seaborn using dataframe

2019-07-11 18:47发布

I am struggling with barplots in seaborn and I am not sure what I am doing wrong. The data is very simple:

name     totalCount
Name1    2000
Name2    40000
Name3    50000

sns.barplot(x='name',y='totalCount',data=df)

produces a bar plot that has mean(totalCount) instead of the actual count.

sns.countplot('name',data=df)

produces a bar plot with all count values on y-axis equal to 1.

How do I generate the plot that has:

totalCount on x-axis, name on y-axis?

1条回答
beautiful°
2楼-- · 2019-07-11 19:12

Usually when plotting a bar chart, a vector of values is passed in per category level, and some function is applied to evaluate each vector to determine the length of each bar.

In your case, you already have a count and just want a bar that's as long as the count value.

It doesn't really matter that Seaborn's default estimator is mean, it should still give you what you're looking for, as it's just taking the mean of a single value, for each category level. The estimation function can be changed by invoking the estimator argument, e.g. estimator=sum, but in this case you don't need to. You just need to change the axis label:

ax = sns.barplot(x='totalCount', y='name', data=df)
ax.set_xlabel('totalCount')

horizontal bar

查看更多
登录 后发表回答