Sorting values in px.violin

hey, guys!

i am plotting some distributions across different states using px.violin and i want to order the stated based on distribution: right now you can see that florida with the biggest variance and higher values is in the middle, while i want it on the first left position and then descending, so second should be colorado, third probably louisiana etc.

i am trying to use
category_orders = {'state': natsorted(true_percentile_df['price per acre_x'].unique())}
but that does not seem to be working. any ideas?)

hi @svrmnnk
Welcome to the community,

Just to ensure I fully understand, you would like to sort the graph by the xaxis (state) or by the yaxis(price per acre)?

hi @adamschroeder !

i would like to sort the graph by yaxis(price per acre), sorting by xaxis(state) works just fine, i manage to sort states alphabetically if i want to, here natsort comes handy, but i struggle with sorting it by y axis.

So what happens if you do?

category_orders={"price per acre_x": natsorted(true_percentile_df['price per acre_x'].unique())}

that did not work, but what worked is just using groupby and then sorting it in a usual way like this:

category_orders = {'state': true_percentile_df.groupby('state').var().sort_values('price per acre_x', ascending = False).index.to_list()}

and got a nice sorted plot

2 Likes