max
max

Reputation: 4521

Plot a pandas categorical Series with Seaborn barplot

I would like to plot the result of the values_counts() method with seaborn, but when I do so, it only shows one of the variables.

df = pd.DataFrame({"A":['b','b','a','c','c','c'],"B":['a','a','a','c','b','d']})
counts = df.A.value_counts()
sns.barplot(counts)

Result of above code

I want a barplot showing heights of 'a' = 1, 'b' = 2, 'c' = 3

I've tried renaming the index and passing in x and y parameters, but I cant' get it to work.

Upvotes: 4

Views: 4115

Answers (1)

sacuL
sacuL

Reputation: 51335

You can do this:

# Sorting indices so it's easier to read 
counts.sort_index(inplace=True)

sns.barplot(x = counts.index, y = counts)
plt.ylabel('counts')

enter image description here

Note that using pandas.Series.plot gives a very similar plot: counts.plot('bar') or counts.plot.bar()

Upvotes: 7

Related Questions