plotting value_counts() in seaborn barplot

In the latest seaborn, you can use the countplot function:

seaborn.countplot(x='reputation', data=df)

To do it with barplot you’d need something like this:

seaborn.barplot(x=df.reputation.value_counts().index, y=df.reputation.value_counts())

You can’t pass 'reputation' as a column name to x while also passing the counts in y. Passing ‘reputation’ for x will use the values of df.reputation (all of them, not just the unique ones) as the x values, and seaborn has no way to align these with the counts. So you need to pass the unique values as x and the counts as y. But you need to call value_counts twice (or do some other sorting on both the unique values and the counts) to ensure they match up right.

Leave a Comment