如何在python xlswriter中将invert_if_negative设置为填充条至纯色。

huangapple go评论62阅读模式
英文:

How to set invert_if_negative to fill bars to a solid color in python xlswriter

问题

我正在使用xls writer从Python在Excel上制作图形,并希望对正值使用绿色条形,对负值使用红色。

当前代码如下:

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'},
    'invert_if_negative': True
})

它在图表中区分正值和负值,但负值只是没有颜色。有办法将反转的颜色设为红色吗?

已尝试使用其他属性如inverted_color或类似的语法,但不起作用。

英文:

I am making graphics on excel from python using xls writer and want to make a graphic with green colored bars for positive values, and red for negative.

Current code seems like this:

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'},
    'invert_if_negative': True
})

It differientiates positive and negative values in the graph but the negative ones are just no-colored. Is there a way to make the inverted color to be red?

Already tried with other properties like inverted_color or any syntax like that but does not work

答案1

得分: 2

你需要使用支持 invert_if_negative_color 参数的 XlsxWriter 版本 >= 3.1.1:

from xlsxwriter import Workbook

workbook = Workbook("chart.xlsx")

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({"type": "column"})

worksheet.write_column("A1", [3, 2, -3, 4, -2])

chart.add_series(
    {
        "values": "=Sheet1!$A$1:$A$5",
        "fill": {"color": "green"},
        "invert_if_negative": True,
        "invert_if_negative_color": "red",
    }
)

worksheet.insert_chart("C1", chart)

workbook.close()

输出:

如何在python xlswriter中将invert_if_negative设置为填充条至纯色。

英文:

You will need version >= 3.1.1 of XlsxWriter which supports the invert_if_negative_color parameter:

from xlsxwriter import Workbook


workbook = Workbook("chart.xlsx")

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({"type": "column"})

worksheet.write_column("A1", [3, 2, -3, 4, -2])

chart.add_series(
    {
        "values": "=Sheet1!$A$1:$A$5",
        "fill": {"color": "green"},
        "invert_if_negative": True,
        "invert_if_negative_color": "red",
    }
)

worksheet.insert_chart("C1", chart)

workbook.close()

Output:

如何在python xlswriter中将invert_if_negative设置为填充条至纯色。

huangapple
  • 本文由 发表于 2023年6月2日 04:07:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定