在Plotly Python中对数据进行排序和排序

2024-04-19 21:09:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在plotly(group条形图)中对数据进行排序,有3列&;一些NaN在它们的行中。要根据每列的最高值(在所有3列中具有最高值)绘制为降序的
我还尝试了下面的代码,但后来我意识到这很愚蠢,因为它只是对列进行排序,所以名字的值最高

bar_plots = [
  go.Bar(x=x, y=plAll['col1'].sort_values(ascending=False), name='col1', marker=go.bar.Marker(color='#0343df')),
  go.Bar(x=x, y=plAll['col2'].sort_values(ascending=False), name='col2', marker=go.bar.Marker(color='#e50000')),
  go.Bar(x=x, y=plAll['col3'].sort_values(ascending=False), name='col3', marker=go.bar.Marker(color='#BEA797'))

fig = go.Figure(data=bar_plots, layout=layout)
]

如图所示,第三组中的一个值高于第二组,但排序不正确。
此图像是通过对数据帧本身进行排序来完成的!想要准确的代码。 enter image description here


Tags: 数据代码namefalsego排序barsort
1条回答
网友
1楼 · 发布于 2024-04-19 21:09:05

它看起来对我很有效:)

plAll = pd.DataFrame({"first": [0, 2, 3, 4, 1, 2], "second": [7, 5, 4, 3, 2, 3],
                      "third": [2, 3, 4, 5, 6, 2]})
x = [2019, 2020, 2021, 2022, 2023]
bar_plots = [
    go.Bar(x=x, y=plAll['first'].sort_values(ascending=True), name='col1', marker=go.bar.Marker(color='#0343df')),
    go.Bar(x=x, y=plAll['second'].sort_values(ascending=False), name='col2', marker=go.bar.Marker(color='#e50000')),
    go.Bar(x=x, y=plAll['third'].sort_values(ascending=False), name='col3', marker=go.bar.Marker(color='#BEA797'))
]
fig = go.Figure(data=bar_plots)
fig.show()

result

这是一篇关于plotly中的组条形图的好文章

https://medium.com/@moritzkoerber/how-to-plot-a-grouped-stacked-bar-chart-in-plotly-df1685b83460

相关问题 更多 >