在 Plotly Go 中叠加而不是堆栈条? [英] Overlay instead of stack bars in Plotly Go?

查看:50
本文介绍了在 Plotly Go 中叠加而不是堆栈条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个堆叠和分组的条形图,有 2 个组,我想覆盖列而不是堆叠.输出如下所示.

I have a stacked and grouped bar chart, with 2 groups, and I'd like to overlay the columns instead of stack. The output is shown below.

代码如下:

import plotly.graph_objects as go
import pandas as pd

stackData = {
    "Leaders Now":[.52, .57, .38, .48],
    "Bottom Now": [.20,  .27, .19, .18],
    # Use differece
    "Leaders Plan": [.17, .06, .12,  .16],
    "Bottom Plan":[.15,.12,.09,.12],
    "labels": [
        "Revenue",
        "Cost",
        "Quality",
        "Flexibility"
    ]
}

# stackData = pd.DataFrame(stackData)

fig3 = go.Figure(
    data=[
        go.Bar(
            name="Leaders Now",
            x=stackData["labels"],
            y=stackData["Leaders Now"],
            offsetgroup=0,
            marker_color = '#024a70',
            text = stackData["Leaders Now"]
        ),
        go.Bar(
            name="Leaders Plan",
            x=stackData["labels"],
            y=stackData["Leaders Plan"],
            offsetgroup=0,
            base=stackData["Leaders Now"],
            marker_color = '#051c2c',
            text= stackData["Leaders Plan"]
        ),
        go.Bar(
            name="Bottom Now",
            x=stackData["labels"],
            y=stackData["Bottom Now"],
            offsetgroup=1,
            marker_color = '#abe5f0',
            text = stackData["Bottom Now"]
        ),
        go.Bar(
            name="Bottom Plan",
            x=stackData["labels"],
            y=stackData["Bottom Plan"],
            offsetgroup=1,
            base=stackData["Bottom Now"],
            marker_color = '#74d0f0',
            text = stackData["Bottom Plan"]
        )
    ],
    layout=go.Layout(
        title="Use Cases",
        yaxis_title="% of Companies"
    )
)

f3 = fig3.full_figure_for_development(warn=False)
fig3.update_traces(texttemplate='%{text:.2%}', textposition='inside')

fig3.show()

因此,如果可能,第一个条形会显示 52%,并且它上面的那个会被覆盖,因此它的值为 69%(而不是 17%).

So if possible, the first bar would say 52%, and the one above it would be overlayed, so it would have value of 69% (instead of 17%).

感谢任何帮助,谢谢!

推荐答案

text 参数更改为类似 text=[round(x+y,2) for x,y在 zip(stackData[Leaders Plan"], stackData[Leaders Now"])] 中,它将两个值相加.

Change the text param to be something like text=[round(x+y,2) for x,y in zip(stackData["Leaders Plan"], stackData["Leaders Now"])] which will sum the two values together.

import plotly.graph_objects as go
import pandas as pd

stackData = {
    "Leaders Now":[.52, .57, .38, .48],
    "Bottom Now": [.20,  .27, .19, .18],
    # Use differece
    "Leaders Plan": [.17, .06, .12,  .16],
    "Bottom Plan":[.15,.12,.09,.12],
    "labels": [
        "Revenue",
        "Cost",
        "Quality",
        "Flexibility"
    ]
}

# stackData = pd.DataFrame(stackData)

fig3 = go.Figure(
    data=[
        go.Bar(
            name="Leaders Now",
            x=stackData["labels"],
            y=stackData["Leaders Now"],
            offsetgroup=0,
            marker_color = '#024a70',
            text = stackData["Leaders Now"]
        ),
        go.Bar(
            name="Leaders Plan",
            x=stackData["labels"],
            y=stackData["Leaders Plan"],
            offsetgroup=0,
            base=stackData["Leaders Now"],
            marker_color = '#051c2c',
            text=[round(x+y,2) for x,y in zip(stackData["Leaders Plan"], stackData["Leaders Now"])]
        ),
        go.Bar(
            name="Bottom Now",
            x=stackData["labels"],
            y=stackData["Bottom Now"],
            offsetgroup=1,
            marker_color = '#abe5f0',
            text = stackData["Bottom Now"]
        ),
        go.Bar(
            name="Bottom Plan",
            x=stackData["labels"],
            y=stackData["Bottom Plan"],
            offsetgroup=1,
            base=stackData["Bottom Now"],
            marker_color = '#74d0f0',
            text = [round(x+y,2) for x,y in zip(stackData["Bottom Plan"], stackData["Bottom Now"])]
        )
    ],
    layout=go.Layout(
        title="Use Cases",
        yaxis_title="% of Companies"
    )
)

f3 = fig3.full_figure_for_development(warn=False)
fig3.update_traces(texttemplate='%{text:.2%}', textposition='inside')

fig3.show()

这篇关于在 Plotly Go 中叠加而不是堆栈条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆