我想更改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()
将legend={'traceOrder':'reverseed'}
添加到update_layout
语句中:
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'},
legend={'traceorder': 'reversed'})
fig.show()