![](/img/trans.png)
[英]Is there any way to implement Stacked or Grouped Bar charts in plotly express
[英]Plotly express: Grouped and stacked of specific data colomns
預期的 output 是一組堆疊圖,但據我所知,沒有簡單的方法可以實現這一點。 因此,我根據從多個索引創建圖形的示例創建了數據結構。 可以在此處找到參考。 我為 x 軸手動創建了多個索引,並為每個索引指定了列。 此圖的限制是列名是必需的。 不能像預期的 output 圖中那樣調整每組的條間距。
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(x=[[2019,2020,2021],['AB','AB','AB']], y=df['A'], name='A'))
fig.add_trace(go.Bar(x=[[2019,2020,2021],['AB','AB','AB']], y=df['B'], name='B'))
fig.add_trace(go.Bar(x=[[2019,2020,2021],['CD','CD','CD']], y=df['C'], name='C'))
fig.add_trace(go.Bar(x=[[2019,2020,2021],['CD','CD','CD']], y=df['D'], name='D'))
fig.update_traces(selector=dict(type='bar'), width=[0.85,0.85,0.85,0.85,0.85,0.85])
fig.update_layout(autosize=False, barmode='stack', bargap=0.0, bargroupgap=0.0, legend=dict(traceorder='normal'))
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.