how to sort values in plotly px.violin

Question:

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.

enter image description here

i’ve been trying to do that with the help of natsort package, doing

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

but that does not seem to be working. any ideas?)

Asked By: ela rednax

||

Answers:

You could use groupBy and sort by var like I’ve done here or define a custom order.

import pandas as pd

df = pd.DataFrame([["jojo", 3], ["marcel", 4], ["jojo", 20], ["jojo", 0.1], ["marcel", 9], ["pierre", 0.9], ["pierre", 0.8], ["pierre", 0.8]], columns=["Name", "Value"])

px.violin(df,
         x="Name",
         y="Value",
         points = "all",
         category_orders={"Name": df.groupby("Name").var().sort_values('Value',ascending=False).index.to_list()})

my results

Hope it’ll help.

Answered By: Virgaux Pierre