在 Jupyter 笔记本中使用plotlyexpress 在堆叠条形图上显示文本值

发布于 2025-01-14 04:03:31 字数 1482 浏览 1 评论 0原文

我正在 Jupyter Notebook 中使用情节表达,制作堆积条形图。 的百分比以显示在图表本身上:

示例数据:

Group1任何基于就业的保险医疗补助其他
61.491.755.515.520.7
Group289.646.328.514.8
Group381.741.228.312.2
Group494.1我正在尝试获取此示例数据中15.816.9

我尝试过 text= 和 textposition = 'auto',如下所示:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

CG = pd.read_csv("CoverageGroups.csv", header=0)

    fig = go.Figure(data=[
    go.Bar(name='Employment Based', x=CG.Group, y=CG['Employment Based'], marker_color='silver', textposition = 'auto'),
    go.Bar(name='Medicaid', x=CG.Group, y=CG['Medicaid'], marker_color='grey', textposition = 'auto'),
    go.Bar(name='Other', x=CG.Group, y=CG['Other'], marker_color='silver', textposition = 'auto'),
])
# Change the bar mode
fig.update_layout(barmode='stack',
                   title='Coverage by Group',
                   xaxis_title='Group',
                   yaxis_title='% Covered',
                   plot_bgcolor='white',
                )
fig.show()

但我的结果图没有根据需要在条上显示任何百分比: 结果图

I am working with plotly express in a Jupyter Notebook, doing a stacked bar chart. I'm trying to get the percentages in this sample data to display on the graph itself:

Sample Data:

GroupAny InsuranceEmployment BasedMedicaidOther
Group191.755.515.520.7
Group289.646.328.514.8
Group381.741.228.312.2
Group494.161.415.816.9

I've tried text= and textposition = 'auto', like below:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

CG = pd.read_csv("CoverageGroups.csv", header=0)

    fig = go.Figure(data=[
    go.Bar(name='Employment Based', x=CG.Group, y=CG['Employment Based'], marker_color='silver', textposition = 'auto'),
    go.Bar(name='Medicaid', x=CG.Group, y=CG['Medicaid'], marker_color='grey', textposition = 'auto'),
    go.Bar(name='Other', x=CG.Group, y=CG['Other'], marker_color='silver', textposition = 'auto'),
])
# Change the bar mode
fig.update_layout(barmode='stack',
                   title='Coverage by Group',
                   xaxis_title='Group',
                   yaxis_title='% Covered',
                   plot_bgcolor='white',
                )
fig.show()

But my resulting figure doesn't have any of the percentages displaying on the bars as desired:
Resulting figure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2025-01-21 04:03:32

您忘记将 text 属性添加到 go.Bar 图中,该图应包含相同的 y 轴值。

完整示例:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

CG = pd.read_csv("CoverageGroups.csv", header=0)

fig = go.Figure()
fig.add_trace(go.Bar(name='Employment Based', x=CG.Group, y=CG['Employment Based'], marker_color='silver',text=CG['Employment Based']))
fig.add_trace(go.Bar(name='Medicaid', x=CG.Group, y=CG['Medicaid'], marker_color='grey', text=CG['Medicaid']))
fig.add_trace(go.Bar(name='Other', x=CG.Group, y=CG['Other'], marker_color='silver', text=CG['Other'])),

fig.update_layout(barmode='stack')
fig.update_traces(textposition = 'inside')
fig.show()

输出
输入图片此处描述

You forgot to add the text attribute to go.Bar plot, which should contain the same value of y-axis.

Full example:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

CG = pd.read_csv("CoverageGroups.csv", header=0)

fig = go.Figure()
fig.add_trace(go.Bar(name='Employment Based', x=CG.Group, y=CG['Employment Based'], marker_color='silver',text=CG['Employment Based']))
fig.add_trace(go.Bar(name='Medicaid', x=CG.Group, y=CG['Medicaid'], marker_color='grey', text=CG['Medicaid']))
fig.add_trace(go.Bar(name='Other', x=CG.Group, y=CG['Other'], marker_color='silver', text=CG['Other'])),

fig.update_layout(barmode='stack')
fig.update_traces(textposition = 'inside')
fig.show()

Output
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文