为了账号安全,请及时绑定邮箱和手机立即绑定

在 plotly express 条形图中反转图例顺序而不更改条形顺序

在 plotly express 条形图中反转图例顺序而不更改条形顺序

慕慕森 2023-02-07 16:39:03
我想更改plotly.express条形图图例中项目的顺序。例如,我想在此图上显示午餐前的晚餐(当前行为对于水平条形图尤其尴尬,因为条形的顺序与图例顺序相反):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 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

添加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()

//img1.sycdn.imooc.com//63e20e610001b97517020808.jpg

查看完整回答
反对 回复 2023-02-07
  • 1 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信