我想更改plotly.express條形圖圖例中項(xiàng)目的順序。例如,我想在此圖上顯示午餐前的晚餐(當(dāng)前行為對(duì)于水平條形圖尤其尷尬,因?yàn)闂l形的順序與圖例順序相反):import plotly.express as pxdf = 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()
1 回答

呼喚遠(yuǎn)方
TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
添加legend={'traceorder': 'reversed'}
到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()
添加回答
舉報(bào)
0/150
提交
取消