Seaborn catplot sort by columns

465 Views Asked by At

I am trying to sort the columns in seaborn catplot and I am unable to perform this operation. I know I can sort the bars within the graph using order= but how to sort the columns? So far, I have written this code for plotting:

top_codes = df["Code"].value_counts()[:5].index.tolist()
top_stations = df["Station"].value_counts()[:5].index.tolist()
sns.catplot(x='Code',col='Station', data=df.loc[(df['Code'].isin(top_codes)) & (df['Station'].isin(top_stations))],
            kind='count', col_wrap=5)

The above code produces the following result: enter image description here

I want station names, e.g., KENNEDY BD STATION, SHEPPHARD WEST STATION, FINCH STATION to appear in alphabetical order.

1

There are 1 best solutions below

0
On

A simple ascending sort is very easy: just call the sorted() function. Add code_order=sorted(top_stations) as parameter to sns.catplot like this:

top_codes = df["Code"].value_counts()[:5].index.tolist()
top_stations = df["Station"].value_counts()[:5].index.tolist()
sns.catplot(x='Code',col='Station', data=df.loc[(df['Code'].isin(top_codes)) & (df['Station'].isin(top_stations))],
        kind='count', col_wrap=5, code_order=sorted(top_stations))