Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn.countplot : order categories by count?

I know that seaborn.countplot has the attribute order which can be set to determine the order of the categories. But what I would like to do is have the categories be in order of descending count. I know that I can accomplish this by computing the count manually (using a groupby operation on the original dataframe, etc.) but I am wondering if this functionality exists with seaborn.countplot. Surprisingly, I cannot find an answer to this question anywhere.

like image 846
nfernand Avatar asked Oct 07 '17 18:10

nfernand


People also ask

Can SNS Countplot () be used to see the frequency distribution of a categorical variable?

countplot. Show the counts of observations in each categorical bin using bars. A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable.

How do you count in Seaborn?

countplot() method is used to Show the counts of observations in each categorical bin using bars. Parameters : This method is accepting the following parameters that are described below: x, y: This parameter take names of variables in data or vector data, optional, Inputs for plotting long-form data.

What does SNS Countplot () do?

When you use sns. countplot , Seaborn literally counts the number of observations per category for a categorical variable, and displays the results as a bar chart.


2 Answers

This functionality is not built into seaborn.countplot as far as I know - the order parameter only accepts a list of strings for the categories, and leaves the ordering logic to the user.

This is not hard to do with value_counts() provided you have a DataFrame though. For example,

import pandas as pd import seaborn as sns import matplotlib.pyplot as plt  sns.set(style='darkgrid')  titanic = sns.load_dataset('titanic') sns.countplot(x = 'class',               data = titanic,               order = titanic['class'].value_counts().index) plt.show() 

enter image description here

like image 148
miradulo Avatar answered Sep 16 '22 17:09

miradulo


Most often, a seaborn countplot is not really necessary. Just plot with pandas bar plot:

import seaborn as sns; sns.set(style='darkgrid') import matplotlib.pyplot as plt  df = sns.load_dataset('titanic')  df['class'].value_counts().plot(kind="bar")  plt.show() 
like image 34
ImportanceOfBeingErnest Avatar answered Sep 17 '22 17:09

ImportanceOfBeingErnest