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

如何在Python XlsxWriter中设置invert_if_negative使负柱形填充红色

解决XlsxWriter柱形图正负值分别设置颜色的问题

你当前的代码里,invert_if_negative=True只会让负值柱形反转填充状态(变为无填充),要指定负值的填充颜色,需要在fill字典中添加negative_color属性,而非尝试inverted_color这类不存在的参数。

修改后的代码如下:

chart3 = workbook.add_chart({'type': 'column'})
chart3.add_series({
    'values': '=Summary!$W$2:$W$76',
    'categories': '=Summary!$A$2:$A$76',
    'gap': 4,
    'line': {'width': 1},
    'name': '=Summary!$W$1',
    'fill': {
        'color': 'green',
        'negative_color': 'red'
    },
    'invert_if_negative': True
})
  • fill.color设置正值柱形的填充色为绿色
  • fill.negative_color专门指定负值柱形的填充色为红色
  • 保留invert_if_negative=True触发正负值的颜色区分逻辑

这样修改后,生成的Excel图表中,正值柱形会显示绿色,负值柱形会显示红色,而非无填充状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 02:15:02