python写入excel(xlswriter)--生成图表

折线图

# -*- 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()

柱状图:

# -*- 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()

效果图

PS:

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

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

三、饼图:

# -*- 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()

效果图:

参考资料:

http://xlsxwriter.readthedocs.io/chart_examples.html

http://xlsxwriter.readthedocs.io/chart.html

python 生成图表的更多相关文章

  1. python xlsxwriter库生成图表的应用

    xlsxwriter可能用过的人并不是很多,不过使用后就会感觉,他的功能让你叹服,除了可以按要求生成你所需要的excel外 还可以加上很形象的各种图,比如柱状图.饼图.折线图等. 请看本人生成的: 这 ...

  2. 【python】使用plotly生成图表数据

    安装 在 ubuntu 环境下,安装 plotly 很简单 python 版本2.7+ pip install plotly 绘图 在 plotly 网站注册后,可以直接将生成的图片保存到网站上,便于 ...

  3. python写入excel(xlswriter)--生成图表

    一.折线图: # -*- coding:utf-8 -*- import xlsxwriter # 创建一个excel workbook = xlsxwriter.Workbook("cha ...

  4. python生成Excel图表(通过xlsxwriter)

    前面介绍了pandas的简单用法,pandas的数据可以通过matlab第三方包将数据生成报表,但是我想将报表生成在Excel中,这时候就可以借助xlsxwriter第三方包来生成图标   缺点:xl ...

  5. JavaScript 解析 Django Python 生成的 datetime 数据 时区问题解决

    JavaScript 解析 Django/Python 生成的 datetime 数据 当Web后台使用Django时,后台生成的时间数据类型就是Python类型的. 项目需要将几个时间存储到数据库中 ...

  6. 关于使用FusionCharts生成图表时出现invalid xml data错误提示的解决方法

    FusionCharts的确功能是够强大的.收集的功能估计更强大.在初次使用时,对着手册,一步一步操作,就是生成图表工具不成功.一直报"Invalid xml data"错误.后面 ...

  7. Python 绘制图表之我见 ---一个java程序员的看法

    ---------------- 环境: win 10  . python3.5 https://github.com/Leechen2014/1400OS_01_Codes/blob/master/ ...

  8. [转] C#操作EXCEL,生成图表的全面应用

    gailzhao 原文 关于C#操作EXCEL,生成图表的全面应用 近来我在开发一个运用C#生成EXCEL文档的程序,其中要根据数据生成相应的图表,该图表对颜色和格式都有严格的要求,在百度和谷歌中搜索 ...

  9. 【python】【转】Python生成随机数的方法

    如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文 ...

随机推荐

  1. UIFont字体大全

    原文地址:UIFont 设置字体作者:青竹居士     http://deep-fish.iteye.com/blog/1678874UIFont 设置字体 1 label.font = [UIFon ...

  2. Jquery——hover与toggle

    hover方法的语法结构为:hover(enter,leave) hover()当鼠标移动到元素上时,会触发第一个方法,当鼠标移开的时候会触发第二个方法 复制代码 <html> <h ...

  3. fread/fwrite

    fread/fwrite第二个参数和第三个参数的区别以及与返回值的关系 size_t fwrite_unlocked(const void * __restrict ptr, size_t size, ...

  4. 【树莓派】在树莓派的Android系统中安装APK应用

    树莓派3 Android TV安装APK应用教程 本文摘自:http://www.mz6.net/news/android/6867.html 树莓派3 Android TV怎样安装软件?对于熟悉AD ...

  5. ubuntu中apache2的安装与卸载

    一.装apache2 安装命令:sudo apt-get install apache2 启动/停止/重启apache2: service apache2 start/stop/restart 二. ...

  6. 使用Phantomjs和ChromeDriver添加Cookies的方法

    一.查看代码 : namespace ToutiaoSpider { class Program { static void Main(string[] args) { var db = Db.Get ...

  7. Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL

    1:Winform应用通过mshtml操作IE浏览器DOM时,第一次运行正常,点击第二次时错误信息如下 A first chance exception of type 'System.Runtime ...

  8. [转]mysql组合索引与字段顺序

    下列转自:http://www.tech-q.cn/archiver/tid-11673.html 很多时候,我们在mysql中创建了索引,但是某些查询还是很慢,根本就没有使用到索引!一般来说,可能是 ...

  9. Python实现微信扫码支付模式二(NativePay)

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7649207.html 核心代码github地址:https://github.com/ygj0930/Pyth ...

  10. insert-interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...