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:
I want station names, e.g., KENNEDY BD STATION, SHEPPHARD WEST STATION, FINCH STATION to appear in alphabetical order.
A simple ascending sort is very easy: just call the
sorted()
function. Addcode_order=sorted(top_stations)
as parameter tosns.catplot
like this: