在 Jupyter 笔记本中使用plotlyexpress 在堆叠条形图上显示文本值
我正在 Jupyter Notebook 中使用情节表达,制作堆积条形图。 的百分比以显示在图表本身上:
示例数据:
Group1 | 任何 | 基于就业的 | 保险医疗补助 | 其他 |
---|---|---|---|---|
61.4 | 91.7 | 55.5 | 15.5 | 20.7 |
Group2 | 89.6 | 46.3 | 28.5 | 14.8 |
Group3 | 81.7 | 41.2 | 28.3 | 12.2 |
Group4 | 94.1 | 我正在尝试获取此示例数据中 | 15.8 | 16.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:
Group | Any Insurance | Employment Based | Medicaid | Other |
---|---|---|---|---|
Group1 | 91.7 | 55.5 | 15.5 | 20.7 |
Group2 | 89.6 | 46.3 | 28.5 | 14.8 |
Group3 | 81.7 | 41.2 | 28.3 | 12.2 |
Group4 | 94.1 | 61.4 | 15.8 | 16.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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记将
text
属性添加到go.Bar
图中,该图应包含相同的 y 轴值。完整示例:
输出

You forgot to add the
text
attribute togo.Bar
plot, which should contain the same value of y-axis.Full example:
Output
