gpt4 book ai didi

python-3.x - Plotly – 按数字 y 对垂直堆积条形图进行排序

转载 作者:行者123 更新时间:2023-12-03 21:04:14 27 4
gpt4 key购买 nike

我需要能够根据每个堆栈中的数字 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

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

最佳答案

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

关于python-3.x - Plotly – 按数字 y 对垂直堆积条形图进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com