一、折线图:

# -*- 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)--生成图表的更多相关文章

  1. python 写入Excel

     一.安装xlrd模块: 1.mac下打开终端输入命令: pip install XlsxWriter 2.验证安装是否成功: 在mac终端输入 python  进入python环境 然后输入 imp ...

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

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

  3. python写入Excel

    一.dataframe存入Excel中: 注意:openpyxl打开的文件需是xlsx的后缀,因为比较新的. from openpyxl import load_workbook import pan ...

  4. python读取excel表格生成sql语句 第一版

    由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦  作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd li ...

  5. python 写入excel数据而不改变excel原有样式

    目标:python写数据到excel,不改变原有样式 解决:在打开excel时,加入该参数formatting_info=True

  6. python写入excel(方式1)

    import xlsxwriter li=["张三","李四","王五","周六","王琪",&qu ...

  7. python写入excel(方式二待完善)

    import xlsxwriter #创建一个工作簿并添加一张工作表,当然工作表是可以命名的# workbook = xlsxwriter.Workbook('Expenses01.xlsx')# w ...

  8. Python 写入excel时的字体格式设置

    转自:https://blog.csdn.net/kuangzhi9124/article/details/81940919 下面代码设置了单元格的字体.位置居中.框线,可以将格式调成自己需要的 im ...

  9. Python操作excel,及图表展示

    学习:http://www.cnblogs.com/Lands-ljk/p/5444619.html

随机推荐

  1. asp grid 增加和删除行数据

    <table border="0" cellpadding="0" cellspacing="0" style="width ...

  2. nginx安装,运行(ubuntu)

    文本只涉及单节点nginx 安装gcc g++依赖库 apt-get install build-essential apt-get install libtool 安装pcre依赖库 apt-get ...

  3. 007 使用SpringMVC开发restful API五--异常处理

    一:任务 1.任务 Spring Boot中默认的错误机制处理机制 自定义异常处理 二:Spring Boot中的默认错误处理机制 1.目前 浏览器访问的时候, restful 接口主要是根据状态码进 ...

  4. 20165235 实验一 Java开发环境的熟悉

    20165235 实验一 Java开发环境的熟悉 课程:JAVA程序设计 姓名:祁瑛 学号:20165235 指导老师:娄嘉鹏 实验日期: 2018.4.2 实验内容:java开发环境的熟悉 一,实验 ...

  5. es6的模块化--AMD/CMD/commonJS/ES6

    , ); ); }) , )); }); , )); ; export { firstName, lastName, year }; // es6引用 import { firstName, last ...

  6. L - Ray in the tube Gym - 101911L (暴力)

    ---恢复内容开始--- You are given a tube which is reflective inside represented as two non-coinciding, but ...

  7. 大家的备忘录——xpage_在同一页面展开文档显示该文档详细信息(可显示处理过的Rich Text)

    Xpage大纲: 解析: 1.通过[link2]点击触发jQuery事件来展开[面板:thispanel] 2.[面板:thispanel]写了onClientLoad事件:让thispanel隐藏. ...

  8. CLR Via第一 章 知识点整理(4) FCL、CTS、CLI和CLS

    FCL(Framework Class Library) Framework 类库: FCL是 .net Framework 包含的一组DLL程序集的统称,FCL包含了提供了很多功能,关于这一部分没有 ...

  9. SpringMVC(二七) 自定义视图

    可以参考博客http://www.cnblogs.com/parryyang/p/5683600.html,举例很清晰. 对自定义的视图名称匹配不同的解析器进行解析. 作用:自己定义视图,视图继承vi ...

  10. DataGridView修改数据并传到数据库

    1. 两个属性设置: 第一个:设置自动创建列,默认为True DataGridView1. AutoGenerateColumns = True; 虽然默认为True,但写下去总是好的!!! 第二个: ...