简体   繁体   English

如何在 plotly 瀑布图中为柱设置不同的 colors?

[英]How to set different colors for bars in a plotly waterfall chart?

I have a waterfall chart and I want to set each bar's color separately (blue for the first one, red for the 2nd, 3rd, and 4th one, green for 5th one, and blue for 6th one).我有一个瀑布图,我想分别设置每个条形的颜色(第一个为蓝色,第二个、第三个和第四个为红色,第五个为绿色,第六个为蓝色)。 All the relative bars in the chart are increasing, and the plotly only allows you to set three colors for increasing, decreasing, and total ones.图表中所有的相关柱都是递增的,而plotly只允许你设置三个colors分别为递增、递减和总计。 Is there any way to do what I want?有什么办法可以做我想做的事吗?

import plotly.graph_objects as go

fig = go.Figure(go.Waterfall(
    name = "20", orientation = "v",
    measure = ["relative", "relative", "relative", "relative", "relative", "total"],
    x = ["Buy", "Transaction Cost", "Remodeling Cost", "Ownership Cost", "Gain", "Sell"],
    textposition = "outside",
    text = ["$200", "$14", "$45", "$5", "$86", "$350"],
    y = [200, 14, 45, 5, 86, 350],
    connector = {"visible": False}
))
fig.show()

Result:结果: 在此处输入图像描述

As I said, I want the color of the bar to be:正如我所说,我希望条形图的颜色为:

blue for the first one, red for the 2nd, 3rd, and 4th one, green for 5th one, and blue for 6th one

Problem问题

Ploty waterfall chart bar color customization. Ploty 瀑布图栏颜色自定义。 As OP mentioned, currently plotly supports customizing bar colors for decreasing, increasing, and totals.正如OP所提到的,目前 plotly 支持自定义栏 colors 用于减少,增加和总计。

Solution解决方案

In OP's example, to make color of bars (blue, red, red, red, green, blue):在 OP 的示例中,制作条形颜色(蓝色、红色、红色、红色、绿色、蓝色):

  • set marker color red in increasing attributeincreasing属性中将标记颜色设置为红色
  • set marker color blue in totals attributetotals属性中设置标记颜色蓝色
  • add shapes of blue and green to the 1st and 4th bar via .add_shape()通过.add_shape()将蓝色和绿色的形状添加到第 1 条和第 4 条
import plotly.graph_objects as go

fig = go.Figure(go.Waterfall(
    name = "20", orientation = "v",
    measure = ["relative", "relative", "relative", "relative", "relative", "total"],
    x = ["Buy", "Transaction Cost", "Remodeling Cost", "Ownership Cost", "Gain", "Sell"],
    textposition = "outside",
    text = ["$200", "$14", "$45", "$5", "$86", "$350"],
    y = [200, 14, 45, 5, 86, 350],
    increasing = {"marker":{"color":"red"}},
    totals = {"marker":{"color":"blue"}},
    connector = {"visible": False}
))

fig.add_shape(
    type="rect", fillcolor="blue", line=dict(color="blue"), opacity=1,
    x0=-0.4, x1=0.4, xref="x", y0=0.0, y1=fig.data[0].y[0], yref="y"
)

fig.add_shape(
    type="rect", fillcolor="green", line=dict(color="green"), opacity=1,
    x0=3.6, x1=4.4, xref="x",
    y0=fig.data[0].y[-1] - fig.data[0].y[-2], y1=fig.data[0].y[-1], yref="y"
)

fig.show()

Which would yield the result OP wanted这将产生 OP 想要的结果带有 op 想要的颜色的瀑布图

Reference参考

I know this is an old question.我知道这是一个老问题。 But I keep running into it and having issues applying the shape when each marker is dynamic.但是我一直遇到它并且在每个标记都是动态的时候应用形状时遇到问题。 So I figured out a way to handle this with css.所以我想出了一个用 css 来处理这个问题的方法。

g.point:nth-of-type(1) path {
fill: blue !important;
stroke: blue !important;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM