python - 在 Plotly Go 中叠加而不是堆栈栏?
问题描述
我有一个堆叠和分组的条形图,有 2 个组,我想覆盖列而不是堆叠。输出如下所示。
代码如下:
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()
所以如果可能的话,第一个柱子会说 52%,它上面的那个会被覆盖,所以它的值是 69%(而不是 17%)。
任何帮助表示赞赏,谢谢!
解决方案
将参数更改为text
类似于text=[round(x+y,2) for x,y in zip(stackData["Leaders Plan"], stackData["Leaders Now"])]
将两个值相加的值。
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=[round(x+y,2) for x,y in zip(stackData["Leaders Plan"], stackData["Leaders Now"])]
),
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 = [round(x+y,2) for x,y in zip(stackData["Bottom Plan"], stackData["Bottom Now"])]
)
],
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()
推荐阅读
- visual-studio - 如何将 VSCode 键绑定导入 Visual Studio?
- css - 如何将链接对齐到中心?
- events - 事件跟踪中的 fieldsObject 是什么?
- python - 熊猫如何在聚合另一列时按组大小对groupby进行排序
- parallel-processing - 如何减少 OpenCL 排队时间/任何其他想法?
- linux - 在 linux 上安装 snorby 时遇到的另一个错误
- java - 如何在一个表达式中将可空字符串与多个值进行比较?
- javascript - 格式化 JSON 以形成分组数组
- javascript - 无法设置未定义的属性“addUser”
- javascript - 自定义组件,错误编译模板