I'd like to change the order of the items in the legend of a plotly.express
bar plot.
For example, I'd like to show Dinner before Lunch on this plot (the current behavior is especially awkward with horizontal bar plots, since the order of the bars is the opposite of the legend order):
import plotly.express as px
df = px.data.tips()
# Sort to put dinner on top.
df.sort_values('time', ascending=False, inplace=True)
fig = px.bar(df, y='sex', x='total_bill', color='time', barmode='group',
orientation='h')
fig.update_layout(yaxis={'categoryorder': 'total ascending'})
fig.show()
plotly.express
and while it mentionstraceorder
it doesn't suggest the simple answer I provided,legend={'traceorder': 'reversed'}
. – Ferriage