Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal barplot in Seaborn using dataframe

Tags:

python

seaborn

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?

like image 335
Anastasia Avatar asked Apr 22 '17 17:04

Anastasia


Video Answer


1 Answers

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

like image 121
andrew_reece Avatar answered Oct 01 '22 17:10

andrew_reece