我想要更改 plotly.express
条形图图例中项目的顺序。
例如,我想在此图表中将晚餐显示在午餐之前(当前的行为尤其在水平条形图中很尴尬,因为条形的顺序与图例的顺序相反):
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
,虽然提到了traceorder
,但它并没有提供我所提供的简单答案,即legend={'traceorder': 'reversed'}
。 - Max Ghenis