Plotly:带有多个轴的分组条形图

时间:2015-03-14 06:14:25

标签: python plot bar-chart plotly multiple-axes

当我在barmode='group' Layout trace2 = Bar(...,yaxis='y2')时设置{{1}}时,会导致条形图堆叠或叠加而不是对它们进行分组。如何在有多个轴的情况下对条形图进行分组?

我接过这些但无济于事:

  • 使用单个Y轴分组条形图显示here
  • 还解释了多个轴here,并提供了y轴参考here

3 个答案:

答案 0 :(得分:2)

对于现在遇到这篇文章的人,plotly现在在条形图中具有offsetgroup属性,可以解决此问题。仍然无法设置barmode='grouped'

答案 1 :(得分:2)

我希望下面基于zoo example的代码是不言自明的,但是您必须在yaxis对象中设置offsetgroupgo.Bar()参数,并且yaxis2对象的layout参数中的go.Figure()参数正确。代码如下:

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure(
    data=[
        go.Bar(name='SF Zoo', x=animals, y=[200, 140, 210], yaxis='y', offsetgroup=1),
        go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29], yaxis='y2', offsetgroup=2)
    ],
    layout={
        'yaxis': {'title': 'SF Zoo axis'},
        'yaxis2': {'title': 'LA Zoo axis', 'overlaying': 'y', 'side': 'right'}
    }
)

# Change the bar mode
fig.update_layout(barmode='group')
fig.show()

结果如下:

enter image description here

答案 2 :(得分:0)

以下是具有多个轴的分组条形图的示例:https://plot.ly/~etpinard/2080/grouped-bars-on-multiple-axes/

可以在此处找到相应的python代码:https://plot.ly/~etpinard/2080/grouped-bars-on-multiple-axes.py

希望这有帮助。