我知道 seaborn.countplot
具有属性 order
可以设置来确定类别的顺序。但我想做的是让类别按降序排列。我知道我可以通过手动计算计数来完成此操作(在原始数据帧上使用 groupby
操作等),但我想知道 seaborn.countplot
是否存在此功能>。令人惊讶的是,我在任何地方都找不到这个问题的答案。
最佳答案
此功能未内置于 seaborn.countplot
据我所知 - order
参数只接受类别的字符串列表,并将排序逻辑留给用户。
这对 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()
关于python - Seaborn.countplot : order categories by count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46623583/