You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python运行Plotly瀑布图脚本报错:color属性值无效

解决Plotly Waterfall图的ValueError问题

错误原因分析

从报错信息和代码中可以定位两个关键问题:

  • 颜色格式非法:waterfall.connector.line.color收到无效值'rgb(63, 63, 63))',字符串末尾多了一个多余的右括号,不符合Plotly要求的RGB颜色格式规范。
  • 列表语法错误:text列表中第三个元素"" "-40"缺少逗号分隔,会触发Python语法解析错误。

修改方案

对代码做两处针对性调整:

  1. 移除颜色字符串末尾的多余括号,将"rgb(63, 63, 63))"改为"rgb(63, 63, 63)"
  2. 在text列表的空字符串和"-40"之间添加逗号,修正列表元素的分隔问题

修改后的完整代码

import plotly.graph_objects as go

fig = go.Figure(go.Waterfall(
    name = "20", orientation = "v",
    measure = ["relative", "relative", "total", "relative", "relative", "total"],
    x = ["Sales", "Consulting", "Net revenue", "Purchases", "Other Expenses", "Profit Before Tax"],
    textposition = "outside",
    text = ["+60", "+80", "", "-40", "-20", "Total"],
    y = [60, 80, 0, -40, -20, 0],
    connector = {"line":{"color":"rgb(63, 63, 63)"}},))
    

fig.update_layout(
        title = "Profit and loss statement 2018",
        showlegend = True
)

    
fig.show()

内容的提问来源于stack exchange,提问作者IbraProjects

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 13:27:14