2017-08-07 56 views
-2

基於在openpyxl值(負或正值)如何更改圖表的顏色?
真的很難爲我。如何根據openpyxl中的值(負值或正值)更改圖表顏色?

from openpyxl import Workbook 
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill,Alignment, Color, numbers 
from openpyxl.chart import BarChart, Reference 

book = Workbook() 
sheet = book.active 
rows = [ 
['Number', 'Batch 1'], 
['a', 40], 
['b', -40], 
['c', 50], 
['d', -30], 
['e', -25], 
['f', 30], 
] 

for row in rows: 
    sheet.append(row) 

chart = BarChart() 
chartCats = Reference(sheet, min_col=1, min_row=2, max_row=7) 
chartData = Reference(sheet, min_col=2, min_row=1, max_col=2, max_row=7) 
chart.add_data(chartData, titles_from_data = True) 
chart.set_categories(chartCats) 
chart.type = 'bar' 
chart.style = 27 
chart.height = 10 
chart.width = 15 
chart.legend = None 
chart.x_axis.tickLblPos = "low" 
chart.y_axis.number_format = '#,##0' 
chart.y_axis.majorGridlines = None 
sheet.add_chart(chart, 'A1') 
book.save('test.xlsx') 

上面的代碼創建標題爲「Batch1」的圖表。但我想要「Batch2」,其中負值和正值顯示不同的顏色。

+1

你能證明在*解決這個自己的任何努力*?甚至任何代碼顯示您正在嘗試執行此操作的上下文? –

+0

從這個(正值和負值共享相同的顏色) ![圖片1](https://i.stack.imgur.com/QRRmk.png) 對這一個(不同的顏色) ![圖片2]( https://i.stack.imgur.com/QURbT.png) – JJJ

+1

這只是圖片說明你的*問題*;你沒有向*解決方案*展示任何努力,也沒有你需要修改的代碼的任何細節。 –

回答