python写入excel表格数据绘制图表_python写入excel(xlswriter)--生成图表

本文介绍了如何使用Python的xlsxwriter库创建Excel文件并绘制不同类型的图表,包括折线图、柱状图和饼图。通过示例代码详细展示了如何准备数据、设置图表属性以及将图表插入到工作表中。
摘要由CSDN通过智能技术生成

一、折线图:

1f6490fcb202e3ad71f6d29ff93fd785.gif

# -*- coding:utf-8 -*-

import xlsxwriter

# 创建一个excel

workbook = xlsxwriter.Workbook("chart_line.xlsx")

# 创建一个sheet

worksheet = workbook.add_worksheet()

# worksheet = workbook.add_worksheet("bug_analysis")

# 自定义样式,加粗

bold = workbook.add_format({'bold': 1})

# --------1、准备数据并写入excel---------------

# 向excel中写入数据,建立图标时要用到

headings = ['Number', 'testA', 'testB']

data = [

['2017-9-1', '2017-9-2', '2017-9-3', '2017-9-4', '2017-9-5', '2017-9-6'],

[10, 40, 50, 20, 10, 50],

[30, 60, 70, 50, 40, 30],

]

# 写入表头

worksheet.write_row('A1', headings, bold)

# 写入数据

worksheet.write_column('A2', data[0])

worksheet.write_column('B2', data[1])

worksheet.write_column('C2', data[2])

# --------2、生成图表并插入到excel---------------

# 创建一个柱状图(line chart)

chart_col = workbook.add_chart({'type': 'line'})

# 配置第一个系列数据

chart_col.add_series({

# 这里的sheet1是默认的值,因为我们在新建sheet时没有指定sheet名

# 如果我们新建sheet时设置了sheet名,这里就要设置成相应的值

'name': '=Sheet1!$B$1',

'categories': '=Sheet1!$A$2:$A$7',

'values': '=Sheet1!$B$2:$B$7',

'line': {'color': 'red'},

})

# 配置第二个系列数据

chart_col.add_series({

'name': '=Sheet1!$C$1',

'categories': '=Sheet1!$A$2:$A$7',

'values': '=Sheet1!$C$2:$C$7',

'line': {'color': 'yellow'},

})

# 配置第二个系列数据(用了另一种语法)

# chart_col.add_series({

# 'name': ['Sheet1', 0, 2],

# 'categories': ['Sheet1', 1, 0, 6, 0],

# 'values': ['Sheet1', 1, 2, 6, 2],

# 'line': {'color': 'yellow'},

# })

# 设置图表的title 和 x,y轴信息

chart_col.set_title({'name': 'The xxx site Bug Analysis'})

chart_col.set_x_axis({'name': 'Test number'})

chart_col.set_y_axis({'name': 'Sample length (mm)'})

# 设置图表的风格

chart_col.set_style(1)

# 把图表插入到worksheet并设置偏移

worksheet.insert_chart('A10', chart_col, {'x_offset': 25, 'y_offset': 10})

workbook.close()

72fd097e11e0f1bc870a3a9a3435a628.gif

效果图:

3f6026b7b77a034141df32c89ae718b4.png

二、柱状图:

db5fff887f878c91253bda3f3080ae7f.gif

# -*- coding:utf-8 -*-

import xlsxwriter

# 创建一个excel

workbook = xlsxwriter.Workbook("chart_column.xlsx")

# 创建一个sheet

worksheet = workbook.add_worksheet()

# worksheet = workbook.add_worksheet("bug_analysis")

# 自定义样式,加粗

bold = workbook.add_format({'bold': 1})

# --------1、准备数据并写入excel---------------

# 向excel中写入数据,建立图标时要用到

headings = ['Number', 'testA', 'testB']

data = [

['2017-9-1', '2017-9-2', '2017-9-3', '2017-9-4', '2017-9-5', '2017-9-6'],

[10, 40, 50, 20, 10, 50],

[30, 60, 70, 50, 40, 30],

]

# 写入表头

worksheet.write_row('A1', headings, bold)

# 写入数据

worksheet.write_column('A2', data[0])

worksheet.write_column('B2', data[1])

worksheet.write_column('C2', data[2])

# --------2、生成图表并插入到excel---------------

# 创建一个柱状图(column chart)

chart_col = workbook.add_chart({'type': 'column'})

# 配置第一个系列数据

chart_col.add_series({

# 这里的sheet1是默认的值,因为我们在新建sheet时没有指定sheet名

# 如果我们新建sheet时设置了sheet名,这里就要设置成相应的值

'name': '=Sheet1!$B$1',

'categories': '=Sheet1!$A$2:$A$7',

'values': '=Sheet1!$B$2:$B$7',

'line': {'color': 'red'},

})

# 配置第二个系列数据(用了另一种语法)

chart_col.add_series({

'name': '=Sheet1!$C$1',

'categories': '=Sheet1!$A$2:$A$7',

'values': '=Sheet1!$C$2:$C$7',

'line': {'color': 'yellow'},

})

# 配置第二个系列数据(用了另一种语法)

# chart_col.add_series({

# 'name': ['Sheet1', 0, 2],

# 'categories': ['Sheet1', 1, 0, 6, 0],

# 'values': ['Sheet1', 1, 2, 6, 2],

# 'line': {'color': 'yellow'},

# })

# 设置图表的title 和 x,y轴信息

chart_col.set_title({'name': 'The xxx site Bug Analysis'})

chart_col.set_x_axis({'name': 'Test number'})

chart_col.set_y_axis({'name': 'Sample length (mm)'})

# 设置图表的风格

chart_col.set_style(1)

# 把图表插入到worksheet以及偏移

worksheet.insert_chart('A10', chart_col, {'x_offset': 25, 'y_offset': 10})

workbook.close()

c727a76823d82c00f2eddda38b1ddfbb.gif

效果图:

cc8daea574a5b6d0191f4aa1c6c88c0d.png

PS:

其实前面两个图只变动一点:把 line 个性为 column

chart_col = workbook.add_chart({'type': 'column'})

三、饼图:

d46193e8f895b047d0e482c288f5696b.gif

# -*- coding:utf-8 -*-

import xlsxwriter

# 创建一个excel

workbook = xlsxwriter.Workbook("chart_pie.xlsx")

# 创建一个sheet

worksheet = workbook.add_worksheet()

# 自定义样式,加粗

bold = workbook.add_format({'bold': 1})

# --------1、准备数据并写入excel---------------

# 向excel中写入数据,建立图标时要用到

data = [

['closed', 'active', 'reopen', 'NT'],

[1012, 109, 123, 131],

]

# 写入数据

worksheet.write_row('A1', data[0], bold)

worksheet.write_row('A2', data[1])

# --------2、生成图表并插入到excel---------------

# 创建一个柱状图(pie chart)

chart_col = workbook.add_chart({'type': 'pie'})

# 配置第一个系列数据

chart_col.add_series({

'name': 'Bug Analysis',

'categories': '=Sheet1!$A$1:$D$1',

'values': '=Sheet1!$A$2:$D$2',

'points': [

{'fill': {'color': '#00CD00'}},

{'fill': {'color': 'red'}},

{'fill': {'color': 'yellow'}},

{'fill': {'color': 'gray'}},

],

})

# 设置图表的title 和 x,y轴信息

chart_col.set_title({'name': 'Bug Analysis'})

# 设置图表的风格

chart_col.set_style(10)

# 把图表插入到worksheet以及偏移

worksheet.insert_chart('B10', chart_col, {'x_offset': 25, 'y_offset': 10})

workbook.close()

d6e822144b89d42d86bad725a7a6c1ba.gif

效果图:

a70cd0212afb224a0f0568dd84142976.png

参考资料:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值