Python Plotly:如何使用go.bar将数据标签添加到堆叠条形图?

Python Plotly:如何使用go.bar将数据标签添加到堆叠条形图?,python,graph,plotly,Python,Graph,Plotly,我有一个堆叠的分组条形图,我想得到图表上每个条形图的每个部分的数据标签 我的图表如下所示: stackData = { "Leaders Now":[.52, .57, .38, .48], "Bottom Now": [.20, .27, .19, .18], # Use differece "Leaders Plan": [.17, .06, .12, .16], "Bottom

我有一个堆叠的分组条形图,我想得到图表上每个条形图的每个部分的数据标签

我的图表如下所示:

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"
    ]
}

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

我只需要条形图上每个项目的%值作为数据标签。plotly express很容易做到这一点,但不确定为什么它不能处理以下内容

fig3.update_traces(texttemplate='%{text:.0%}',textposition='auto')

谢谢大家!

您只需为每个
go.Bar
指定
text
属性,如:

go.Bar(
    name="Leaders Now",
    x=stackData["labels"],
    y=stackData["Leaders Now"],
    offsetgroup=0,
    marker_color = '#024a70',
    text = stackData["Leaders Now"]
)
绘图:

完整代码:
谢谢你接受我的回答。如果你觉得它有用,请考虑一个赞成票。我想知道是否有可能得到酒吧的叠加格式?就像第一根钢筋是52%,上面的钢筋是重叠的,所以它的值是69%(而不是17%)。如有必要,我可以单独提问。谢谢,这是可能的。但是是的,请把它作为一个单独的问题发布。以下是帖子:
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()