Search code examples
pythonpandasplotly-pythoncategorical-data

Order plotly figure by CategoricalDtype order


The goal is to visualize boxplots for each group in order. Group is ofpd.CategoricalDtype and ordered. I do not manage to get plotly to respect the order. Instead, it only sorts alphabetically.

import pandas as pd
import numpy as np
import plotly.express as px

df = pd.DataFrame({'group': list('CAACCBAA'), 'values': np.random.random(8)})
cat_ordered = pd.CategoricalDtype(categories=['B', 'C', 'A'], ordered=True)
df['group'] = df['group'].astype(cat_ordered)

fig = px.box(df, x='group', y='values', 
             category_orders={'group': list(df.group.cat.categories)})
fig.update_xaxes(categoryorder='category ascending')
fig.show()

The expected order would be B - C - A. px.box output

I believe plotly does not take over the order from the pandas dataframe, but manually setting it does not help either.

plotly==5.17.0


Solution

  • Plotly doesn't seem to treat list and Index objects the same way.

    Passing the latter (that is returned by categories) fixes the issue :

    fig = px.box(df, x='group', y='values', template='plotly_dark',
                 category_orders={'group': cat_ordered.categories}) # <-- updated
                                     # or df.group.cat.categories
    
    fig.show();
    

    enter image description here