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
随机推荐
- Collection接口的常用方法
[添加功能] boolean add(Object obj); //添加一个元素. boolean addAll(Collection c); //添加一个集合的元素(在后面追加). [删除功能] v ...
- Linux基础学习(一)__后台运行Python文件
Linux 后台运行Python脚本 1.安装Python:(python 3.5.4) 2.安装Python依赖包: 2.1 处理Python更新后yum无法正常使用的问题 (错误信息: -bash ...
- sentinel-dashboard安装、运行(ubuntu)
下载页面https://github.com/alibaba/Sentinel/releases wget -P /opt/downloads https://github.com/alibaba/S ...
- .netcore加入APM系统 SkyWalking
安装环境:windows 2016 必要条件: JDK8+ Elasticsearch 5.x(注:目前不支持es6) 8080,10800,11800,12800 端口不被占用 下载skywalki ...
- URAL 1989 Subpalindromes (多项式hash) +【线段树】
<题目链接> <转载于 >>> > 题目大意:给你一段字符串,进行两种操作:1.询问[l,r]这个区间中的字符串是否是回文串: 2.更改该字符串中对应下标的 ...
- JVM之基本结构
1. Java虚拟机的架构 1.1 Java的NIO库允许Java程序使用直接内存,访问直接内存的速度优于Java堆.出于性能的考虑,读写频繁的场合会考虑使用直接内存. 1.2 本地方法栈和Java栈 ...
- MySQL安装目录修改
- Element UI 树形表格,TreeGrid或TreeTable实现
安装less npm install less --save-devnpm install less-loader --save-dev 模板代码 <template> <div c ...
- 2109 ACM 排序
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2109 题意:简单的排序问题 代码: #include<cstdio> #include< ...
- 流畅的Python——切片
2.4 切片 在 Python 里,像列表(list).元组(tuple)和字符串(str)这类序列类型都支持切片操作,但是实际上切片操作比人们所想象的要强大很多. 在我个人的使用经历来看,在算法实践 ...