Seaborn.countplot: account order categories?

I know that it seaborn.countplothas an attribute orderthat can be set to determine the order of categories. But I would like the categories to be in descending order. I know that I can do this by calculating the score manually (using the operation groupbyon the original data frame, etc.), but I wonder if this functionality exists with seaborn.countplot. Surprisingly, I just can not find the answer to this question.

+4
source share
2 answers

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

value_counts(), DataFrame. ,

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

+9

, . pandas:

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()
+2

Source: https://habr.com/ru/post/1687084/


All Articles