【问题标题】:Plotly – sort vertical stacked bar graph by numeric yPlotly – 按数字 y 对垂直堆积条形图进行排序
【发布时间】:2019-04-07 00:28:39
【问题描述】:

我需要能够根据每个堆栈中的数字 y 值对堆积条形图进行排序。在 Plotly 的 R 版本中似乎有一个解决这个问题的解决方案,但我还没有找到适用于 Python 的解决方案,或者更具体地说,Plotly Dash。

以下是如何在 R 中解决排序问题的两个示例:

1) Stacked bar graphs in plotly: how to control the order of bars in each stack

2) Ordered bars on plotly

以下是 Plotly 堆叠条形图的基本代码:

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[20, 14, 23],
    name='SF Zoo'
)
trace2 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[12, 18, 29],
    name='LA Zoo'
)

data = [trace1, trace2]
layout = go.Layout(
    barmode='stack',
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='stacked-bar')

这是实际输出:

https://i.imgur.com/32ZLw6P.png

根据 x 轴(分类)对这些条形进行排序很容易。按字母顺序对 x 进行排序所需的唯一代码是:

layout = go.Layout(
    barmode='stack',
    xaxis={'categoryorder': 'category ascending'}
)

但是是否也可以使用 Plotly Dash (Python) 按数字 y 值对每个堆栈进行排序?如果对每个堆叠条进行排序,使得每个堆叠条中的最低 y 值位于条的底部(无论跟踪如何),以下是预期结果:

https://i.imgur.com/7V3CQFQ.png

现在似乎只能对类别进行排序。

【问题讨论】:

    标签: python-3.x graph plotly plotly-dash


    【解决方案1】:

    只需切换跟踪的顺序。 Plotly 绘制轨迹,并根据您编写轨迹的顺序显示图例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-04
      • 2021-10-10
      • 2018-12-21
      • 2018-09-30
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多