✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Violin plot to plot in ascending order

Hi there,
I have created a violin plot but I would like the order of my plots to be in ascending numerical order. Before I plot the ‘data’ to my code I have sorted my data by the x values in the hope that this will help.
data = data.sort_values(by=[x_value])
However, the order of my data is still random and not in ascending order. Any advice on how to fix this issue would be greatly appreciated. I have attached a screenshot of how the plot currently looks like. I would just like to have the values on the x-axis in numerical order.

The problem here is the [10.0-72.0) value, right? you’ll need to sort the strings using a “natural sort” rather than a string sort here basically.

Are you using Plotly Express here, like px.violin or are you building up your figure from graph_objects ?

Hi,
Thank you for your reply,
Im using plotly express here so px.violin. Yes that value is out of place. How would I sort the strings as ‘natural sort’? Do you mind providing me with an example?
Thank you!

Sure, here you go:

import plotly.express as px
import pandas as pd
import numpy as np
from natsort import natsorted

df = pd.DataFrame(dict(
    value = np.random.rand(30),
    bucket = ["[1-5)", "[10-15)", "[5-10)"]*10
))

px.violin(df, x="bucket", y="value", color="bucket",
         category_orders={"bucket": natsorted(df["bucket"].unique())})

This is using the natsort module https://pypi.org/project/natsort/ which sorts strings “naturally” as opposed to strictly lexicographically, so that “10” appears between “1” and “20”.

Thank you for this! Worked perfectly.

With go.Violin it’s more complicated… you have to add the trace in the correct order, and you have to set xaxis.categoryarray I believe. This is the kind of thing that PX takes care of for you basically :slight_smile:

1 Like