在 plotly express 条形图中反转图例顺序而不更改条形顺序
Reverse legend order without changing bar order in plotly express bar plot
我想更改 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': '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()
我想更改 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': '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()