Seaborn.countplot : order categories by count? Seaborn.countplot : order categories by count? pandas pandas

Seaborn.countplot : order categories by count?


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 pdimport seaborn as snsimport matplotlib.pyplot as pltsns.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


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 pltdf = sns.load_dataset('titanic')df['class'].value_counts().plot(kind="bar")plt.show()