Seaborn.countplot : order categories by count?

前端 未结 2 1644
野趣味
野趣味 2021-02-03 19:09

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

2条回答
  •  情话喂你
    2021-02-03 19:42

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

提交回复
热议问题