在 plotly 中更改分组条形图的组内顺序

Changing the within-group order of a grouped bar plot in plotly

我想在 Python 的条形图中更改组内变量的顺序。 例如,我如何在这里调换午餐和晚餐的顺序?

import plotly.express as px
df = px.data.tips()
fig = px.bar(df, x='sex', y='total_bill', color='time', barmode='group')
fig.show()

categoryorder只能改变组的顺序,即男性和女性。

根据列 timedf 进行降序排序。

import plotly.express as px
df = px.data.tips()
df.sort_values(['time'],ascending=[False],inplace=True)
fig = px.bar(df, x='sex', y='total_bill', color='time', barmode='group')
fig.show()

输出: