简体   繁体   中英

Horizontal barplot in Seaborn using dataframe

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?

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, eg 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')

单杠

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM