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写入excel(xlswriter)--生成图表的更多相关文章
- python 写入Excel
一.安装xlrd模块: 1.mac下打开终端输入命令: pip install XlsxWriter 2.验证安装是否成功: 在mac终端输入 python 进入python环境 然后输入 imp ...
- 【python】使用plotly生成图表数据
安装 在 ubuntu 环境下,安装 plotly 很简单 python 版本2.7+ pip install plotly 绘图 在 plotly 网站注册后,可以直接将生成的图片保存到网站上,便于 ...
- python写入Excel
一.dataframe存入Excel中: 注意:openpyxl打开的文件需是xlsx的后缀,因为比较新的. from openpyxl import load_workbook import pan ...
- python读取excel表格生成sql语句 第一版
由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦 作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd li ...
- python 写入excel数据而不改变excel原有样式
目标:python写数据到excel,不改变原有样式 解决:在打开excel时,加入该参数formatting_info=True
- python写入excel(方式1)
import xlsxwriter li=["张三","李四","王五","周六","王琪",&qu ...
- python写入excel(方式二待完善)
import xlsxwriter #创建一个工作簿并添加一张工作表,当然工作表是可以命名的# workbook = xlsxwriter.Workbook('Expenses01.xlsx')# w ...
- Python 写入excel时的字体格式设置
转自:https://blog.csdn.net/kuangzhi9124/article/details/81940919 下面代码设置了单元格的字体.位置居中.框线,可以将格式调成自己需要的 im ...
- Python操作excel,及图表展示
学习:http://www.cnblogs.com/Lands-ljk/p/5444619.html
随机推荐
- Vijos1983 NOIP2015Day2T3 运输计划 transport LCA
题目链接Vijos 题目链接UOJ 该博客在博客园的链接 转载一个大佬的题解: 点击这里->大佬题解 下面谈谈我的感悟: 当然写代码也是写的很艰辛: 我力劝C++的同胞们,这题卡常数,Dfs党会 ...
- linux中查看http各种状态数量
转自: http://www.cnblogs.com/wayne173/p/5652043.html 我们的网站部署在linux的服务器上,特别是web服务器,我们可能有时候做为运维人员,肯定是要查看 ...
- LoadRunner脚本参数化之设置条件与运行结果说明
性能测试中为什么需要进行参数化? 1.功能方面:首先要保证脚本的功能完善.可用性.(一般来说,参数化主要针对业务中的具备唯一性的数据.) 2.性能方面:一般来说,如果服务器存在缓存机制,在测试过程中, ...
- Scrapy-redis 安装配置使用
# 安装redis服务器端 sudo apt-get install redis-server # 安装scrapy和scrapy-redis库 pip install scrapy pip inst ...
- Spring(二)IOC底层实现原理
IOC原理 将对象创建交给Spring去管理. 实现IOC的两种方式 IOC配置文件的方式 IOC注解的方式 IOC底层实现原理 底层实现使用的技术 1.1 xml配置文件 1.2 dom4j解析xm ...
- TensorFlow激活函数+归一化-函数
激活函数的作用如下-引用<TensorFlow实践>: 这些函数与其他层的输出联合使用可以生成特征图.他们用于对某些运算的结果进行平滑或者微分.其目标是为神经网络引入非线性.曲线能够刻画出 ...
- poj-1287 Networking(Prim)
题目链接:http://poj.org/problem?id=1287 题目描述: 请先参考关于prim算法求最小生成树的讲解博客:https://www.cnblogs.com/LJHAHA/p/1 ...
- Masquerade strikes back Gym - 101911D (数学)
Quite often the jury of Saratov SU use the problem "Masquerade" in different practice sess ...
- asp.net结合html使用
在用asp.net开发系统时,用控件能更方便快捷,但是大家也知道用它的控件会导致不可控,特别是css,如何用前端定义的html+css+js来运用asp.net(c#)呢,下面写了一个小实例,实现页面 ...
- POJ1742----Coins
背包专题:http://www.cnblogs.com/qq188380780/p/6409474.html //多重背包 #include<cstdio> ],a[][]; int Ro ...