Plotly Express histogram, any way to sort bar by value?

I am using the Plotly Express Histogram plot to display a groupby sum for a dataframe. If I am using cufflinks, I will normally do

df.groupby('group')['col'].sum().sort_values(ascending=False).iplot(kind='bar')

I realize I can do the same thing using

px.histogram(df, x='group', y='col', histfunc='sum')

However, I don’t know how to sort the bar by its values. Does plotly express support this function?

The next release of Plotly.py and Plotly Express will support this, yes! I’ll post an example in a day or two :slight_smile:

1 Like

@nicolaskruchten, I can’t wait to see the new release of plotly_express. Keep up with the good work.

here you go!

Also works with multiple traces:

Thanks! This is a really clean API! I pip install plotly_express again this morning (I am in China), and for version 0.3.0, I don’t see this feature yet, so I assume will be in the next release?

No, this should work with 0.3.0 … what’s not working such that you “don’t see this feature” yet? Is it returning some kind of error message?

Installing plotly_express==0.3.0 should cause plotly to upgrade to 3.10.0 which includes this feature.

Sorry, a typo on my part cause the error. I type update_xaxis instead of update_xaxes. Again, sorry for the confusion.

Sorry, my bad for pasting screenshots instead of code :pray:

Here’s the code in case others want to paste it later:

px.histogram(tips, x="day", color="smoker").update_xaxes(categoryorder="total descending")

Note that this trick works on all 2d-cartesian categorical axes: you can use it with scatter, bar, line, heatmap, box, violin, etc :slight_smile:

1 Like