首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Plotly:如何使用go.Bar向堆叠条形图添加数据标签?

Plotly:如何使用go.Bar向堆叠条形图添加数据标签?
EN

Stack Overflow用户
提问于 2021-03-06 00:58:14
回答 1查看 1.1K关注 0票数 2

我有一个堆叠的分组条形图,我想要获取该图上每个条形图的每个部分的数据标签。

我的图表如下:

代码语言:javascript
运行
复制
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很容易,但不确定为什么它不能与以下内容一起使用

代码语言:javascript
运行
复制
fig3.update_traces(texttemplate='%{text:.0%}',textposition='auto')

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-09 19:07:15

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

代码语言:javascript
运行
复制
go.Bar(
    name="Leaders Now",
    x=stackData["labels"],
    y=stackData["Leaders Now"],
    offsetgroup=0,
    marker_color = '#024a70',
    text = stackData["Leaders Now"]
)

绘图:

完整代码:

代码语言:javascript
运行
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66496583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档