英文:
Waterfall chart with Plotly - Update Traces
问题
以下是代码的翻译部分:
# 我正在为三个类别创建一个瀑布图,如下所示的代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Waterfall(
x=[["Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1",
"Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2",
"Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3"],
["Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA"]],
measure=["absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total"],
y=[1693, -296, 1501, -897, -27, -45, 532,
1439.05, -251.6, 1275.85, -762.44, -22.95, -38.25, 452.2,
1134.31, -198.32, 1005.67, -600.99, -18.09, -30.150, 356.44]
))
# 代码返回这张图片:https://i.stack.imgur.com/bDf2g.png
# 接下来,我想要编辑“Gross Income”项目的颜色为绿色,只有“EBTIDA”应该有不同的布局。
# 我尝试了以下代码:
fig.update_traces(marker_color="LightSeaGreen", selector=dict(x='Gross Income'))
# 但是它不起作用。有人知道如何做吗?
# 谢谢
这是您提供的代码的翻译,没有其他附加内容。
英文:
I'm creating a waterfall plot for three categories, as shown in below code:
import plotly.graph_objects as go
fig=go.Figure()
fig.add_trace(go.Waterfall(
x = [["Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1",
"Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2",
"Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3"
],
["Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
]
],
measure = ["absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total"
],
y = [
1693,-296,1501,-897,-27,-45,532,
1439.05,-251.6,1275.85,-762.44,-22.95,-38.25,452.2,
1134.31,-198.32,1005.67,-600.99,-18.09,-30.150,356.44
]
))
The code returns this image: https://i.stack.imgur.com/bDf2g.png
What I want to do next is to edit color of 'Gross Income' items to green, so only EBTIDA would present a different layout.
I tried so with:
fig.update_traces(marker_color="LightSeaGreen",selector=dict(x='Gross Income'))
It doesn't work, though. Does anyone know how to do it?
Thanks
答案1
得分: 1
这是非常困难的,因为在Plotly的瀑布图中,标记颜色是根据它们是增加、减少或总和来分配的,无法根据它们的类别来分配颜色。
然而,通过一个非常丑陋的小技巧,我们可以使图表看起来在“总收入”类别中具有所需的颜色。我们可以将总收入的柱状分别绘制在三个类别中,为它们分配相同的值,并将它们分类为“相对”,以便我们可以使用参数increasing = {"marker": {"color": "lightseagreen"}}
来使它们都变为lightseagreen
。注意:这只能因为它们都恰好是正值才有效。
然后,因为我们必须将每个重叠的总收入作为单独的跟踪添加,所以我们需要偏移这些柱以确保它们与原始瀑布图的柱重叠。我只是通过反复试验来确定offset=-0.4
看起来大致正确。由于这些额外的柱仅是视觉效果,我还禁用了它们的悬停信息并防止它们出现在图例中。
import plotly.graph_objects as go
fig=go.Figure()
fig.add_trace(go.Waterfall(
x = [["Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1",
"Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2",
"Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3"
],
["Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
]
],
measure = ["absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total"
],
y = [
1693,-296,1501,-897,-27,-45,532,
1439.05,-251.6,1275.85,-762.44,-22.95,-38.25,452.2,
1134.31,-198.32,1005.67,-600.99,-18.09,-30.150,356.44
]
))
## 在每个类别中添加总收入柱
for category, value in zip(["Category 1", "Category 2", "Category 3"], [1693,-1439.05,1134.31]):
fig.add_trace(go.Waterfall(
x = [category, "Gross Income"],
measure = ["relative"],
y = [value],
increasing = {"marker":{"color":"lightseagreen"}},
offset=-0.4,
connector={"visible":False},
showlegend=False,
hoverinfo='skip',
))
fig.show()
英文:
This is very difficult because for waterfall charts in plotly, the marker colors are assigned based on whether they are increasing, decreasing or total and cannot be assigned colors based on their category.
However, with a very ugly hack, we can make the plot appear to have the desired color in the "gross income" category. We can plot the gross income bars separately for all three categories, assigning them the same value, and classifying them as "relative" so that we can use the argument increasing = {"marker":{"color":"lightseagreen"}}
to make them all lightseagreen
. Note: this only works because they all happen to be positive values.
Then, because we have to add each overlapping gross income as a separate trace, we will need to offset each of these bars to ensure they overlap the bars from your original waterfall figure. I just used trial and error to figure out that offset=-0.4
looks approximately correct. Since these additional bars are purely visual, I also disabled their hover info and prevented them from appearing in the legend.
import plotly.graph_objects as go
fig=go.Figure()
fig.add_trace(go.Waterfall(
x = [["Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1", "Category 1",
"Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2", "Category 2",
"Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3", "Category 3"
],
["Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
"Gross Income", "Taxes", "Net Revenue", "CPV", "Variable Expenses", "Recurrent Capex", "EBITDA",
]
],
measure = ["absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total",
"absolute", "relative", "relative", "relative", "relative", "relative", "total"
],
y = [
1693,-296,1501,-897,-27,-45,532,
1439.05,-251.6,1275.85,-762.44,-22.95,-38.25,452.2,
1134.31,-198.32,1005.67,-600.99,-18.09,-30.150,356.44
]
))
## add the gross income bars in each category
for category, value in zip(["Category 1", "Category 2", "Category 3"], [1693,-1439.05,1134.31]):
fig.add_trace(go.Waterfall(
x = [[category],["Gross Income"]],
measure = ["relative"],
y = [value],
increasing = {"marker":{"color":"lightseagreen"}},
offset=-0.4,
connector={"visible":False},
showlegend=False,
hoverinfo='skip',
))
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论