2017-06-20 176 views
0

我在openpyxl中創建折線圖時遇到問題。 我使用下面的代碼:openpyxl中的折線圖

from datetime import date 
from openpyxl import Workbook 
from openpyxl.chart import LineChart, Reference, Series 

list_of_num = ['Batch 1', 'Batch 2', 'Batch 3'] 

ms_lastc = 3 

rows = [ 
    ['Date', 'Batch 1', 'Batch 2', 'Batch 3'], 
    [date(2015, 9, 1), 40, 30, 25], 
    [date(2015, 9, 2), 40, 25, 30], 
    [date(2015, 9, 3), 50, 30, 45], 
    [date(2015, 9, 4), 30, 25, 40], 
    [date(2015, 9, 5), 25, 35, 30], 
    [date(2015, 9, 6), 20, 40, 35]] 


if __name__ == '__main__': 
    wb = Workbook() 
    ms = wb.active 
    for row in rows: 
     ms.append(row) 
    lc = LineChart() 
    x = Reference(ms, 
        min_col=1, 
        min_row=2, 
        max_col=1, 
        max_row=7) 
    for i, name in enumerate(list_of_num): 
     values = Reference(ms, 
          min_col=i+1, 
          min_row=2, 
          max_col=i+1, 
          max_row=7) 
     serie = Series(values, xvalues=x, title=name) 
     lc.append(serie) 
    ms.add_chart(lc, 'A9') 
    wb.save('bla.xlsx') 

下面的代碼顯示了這樣的結果: result

當我在圖表中打開意甲系列值爲空。

有人可以幫我嗎?

+0

它看起來像你沒有使用正確的文檔爲您有的版本的openpyxl。請參閱http://openpyxl.readthedocs.io/en/2.4/charts/introduction.html#creating-a-chart –

回答

0

使用散點圖來代替:

from openpyxl.chart import ScatterChart 

ls = ScatterChart() 

默認值是散點圖點用線連接,這可能是你想要的。