英文:
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()
输出:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论