【发布时间】:2021-06-04 08:56:21
【问题描述】:
我有一个堆叠的分组条形图,我想获取图表上每个条形的每个部分的数据标签。
我的图表如下:
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')
谢谢!
【问题讨论】:
-
感谢您接受我的回答。如果您觉得它有用,也请考虑点赞。
-
我想知道是否有可能获得覆盖格式的栏?就像第一个柱是 52%,它上面的那个会被覆盖,所以它的值是 69%(而不是 17%)。如有必要,我可以发布一个单独的问题。谢谢
-
这也许是可能的。但是,是的,请将其作为一个单独的问题发布。