【问题标题】:Reverse legend order without changing bar order in plotly express bar plot反转图例顺序而不更改绘图快速条形图中的条形顺序
【发布时间】:2020-10-18 00:50:21
【问题描述】:

我想更改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()

【问题讨论】:

  • 这能回答你的问题吗? Customizing the order of legends in plotly
  • @vestland 我不这么认为,它没有使用plotly.express,虽然它提到了traceorder,但它并没有暗示我提供的简单答案legend={'traceorder': 'reversed'}

标签: python plot plotly plotly-python plotly-express


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2020-10-16
    • 2020-08-24
    相关资源
    最近更新 更多