Python运行Plotly瀑布图脚本报错:color属性值无效
解决Plotly Waterfall图的ValueError问题
错误原因分析
从报错信息和代码中可以定位两个关键问题:
- 颜色格式非法:
waterfall.connector.line.color收到无效值'rgb(63, 63, 63))',字符串末尾多了一个多余的右括号,不符合Plotly要求的RGB颜色格式规范。 - 列表语法错误:
text列表中第三个元素"" "-40"缺少逗号分隔,会触发Python语法解析错误。
修改方案
对代码做两处针对性调整:
- 移除颜色字符串末尾的多余括号,将
"rgb(63, 63, 63))"改为"rgb(63, 63, 63)" - 在
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
相关产品推荐
相关产品推荐

