plotly_express icon indicating copy to clipboard operation
plotly_express copied to clipboard

Infer categorical order from ordered pandas categoricals

Open joelostblom opened this issue 6 years ago • 1 comments

It would be nice if px could infer the order of categorical columns if they are already set in pandas.

species_cat = pd.CategoricalDtype(categories=['virginica', 'versicolor', 'setosa'], ordered=True)
iris['species2'] = iris['species'].astype(species_cat)
px.histogram(iris, 'species2')

image

One approach to solve this might be to check the column data types for all columns mapped to a visual element and if they are ordered pandas categoricals, set the plot order via category_orders to be df['col'].cat.categories. So, essentially automating the below:

px.histogram(iris, 'species2', category_orders={'species2': iris['species2'].cat.categories})

image

I am not sure if there would be any cases when it would be disadvantageous to use the pandas order. At least for x, y, color, and facets I think it makes sense, thoughts?

joelostblom avatar Nov 10 '19 09:11 joelostblom

Yes, this would be very useful

bdwolfe avatar Mar 17 '23 14:03 bdwolfe