Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting series using seaborn

category = df.category_name_column.value_counts()  

I have the above series which returns the values:

CategoryA,100
CategoryB,200

I am trying to plot the top 5 category names in X - axis and values in y-axis

head = (category.head(5)) 
sns.barplot(x = head ,y=df.category_name_column.value_counts(), data=df)

It does not print the "names" of the categories in the X-axis, but the count. How to print the top 5 names in X and Values in Y?

like image 836
Hackerds Avatar asked Nov 29 '22 06:11

Hackerds


1 Answers

You can pass in the series' index & values to x & y respectively in sns.barplot. With that the plotting code becomes:

sns.barplot(head.index, head.values)

I am trying to plot the top 5 category names in X

calling category.head(5) will return the first five values from the series category, which may be different than the top 5 based on the number of times each category appears. If you want the 5 most frequent categories, it is necessary to sort the series first & then call head(5). Like this:

category = df.category_name_column.value_counts()
head = category.sort_values(ascending=False).head(5)
like image 65
Haleemur Ali Avatar answered Dec 06 '22 10:12

Haleemur Ali