利用python向excel文件写数据并绘制折线图

2019-01-06  本文已影响0人  MrCloudPeak

依赖

python 2.7.15
xlswriter(可以使用pip insall xlswriter)

具体实现

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xlsxwriter

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet('sheet_test')

# Some data we want to write to the worksheet.
expenses = (
    ['mode', u'前置', u'主流程', u'后置'],
    ['1rc', 543, 64, 555],
    ['2rc', 346, 36, 65],
    ['3rc', 56, 88, 445],
)

for i, line in enumerate(expenses):
    for j, col in enumerate(line):
        worksheet.write(i, j, col)

chart = workbook.add_chart({'type': 'line'})
chart.set_title({'name': u'多rc'})
chart.set_x_axis({'name': ['sheet_test', 0, 0]})
chart.set_y_axis({'name': u'时间(s)'})
chart.add_series({
    'marker': {'type': 'diamond'},
    'name': ['sheet_test', 1, 0],
    'categories': ['sheet_test', 0, 1, 0, 3],
    'values': ['sheet_test', 1, 1, 1, 3],
})
chart.add_series({
    'marker': {'type': 'diamond'},
    'name': ['sheet_test', 2, 0],
    'categories': ['sheet_test', 0, 1, 0, 3],
    'values': ['sheet_test', 2, 1, 2, 3],
})
chart.add_series({
    'marker': {'type': 'diamond'},
    'name': ['sheet_test', 3, 0],
    'categories': ['sheet_test', 0, 1, 0, 3],
    'values': ['sheet_test', 3, 1, 3, 3],
})
worksheet.insert_chart(len(expenses) + 1, 0, chart)

workbook.close()

运行结果


image.png

参考

https://xlsxwriter.readthedocs.io

上一篇下一篇

猜你喜欢

热点阅读