openpyxl中的折线图

时间:2017-06-20 11:53:18

标签: python excel charts 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

当我在图表中打开Serie时,系列值为空。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

改为使用散点图:

from openpyxl.chart import ScatterChart

ls = ScatterChart()

默认情况下,散点图点按行连接,这可能就是您想要的。