Python openpyxl 插入折线图实例

 更新时间:2020年04月17日 14:34:02   作者:瓦力冫  
这篇文章主要介绍了Python openpyxl 插入折线图实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,还是直接看代码吧!

import os
import openpyxl

from datetime import date
from openpyxl import Workbook

from openpyxl.chart import (
 Series,
 LineChart,
 Reference,
)
def add_line_chart(title, wss, min_col, min_row, max_col, max_row):
 c1 = LineChart()
 c1.title = title # 图的标题
 c1.style = 12 # 线条的style
 c1.y_axis.title = 'percent' # y坐标的标题
 if 'IDC' not in title:
  c1.x_axis.number_format = 'd-mmm' # 规定日期格式 这是月,年格式
  c1.x_axis.majorTimeUnit = "Months" # 规定日期间隔 注意days;Months大写
 c1.x_axis.title = "Date" # x坐标的标题
 data = Reference(wss, min_col=min_col, min_row=min_row, max_col=max_col, max_row=max_row) # 图像的数据 起始行、起始列、终止行、终止列
 c1.add_data(data, titles_from_data=True, from_rows=True)
 dates = Reference(wss, min_col=2, min_row=1, max_col=max_col)
 c1.set_categories(dates)
 wss.add_chart(c1, "A6") # 将图表添加到 sheet中


def save_data_to_excel(file_name, target_sheet_name):
 select_cursor = connect.cursor()
 select_sql = "select phone_company, record_date, record_percent from phone_statistics where record_company = '%s'" % target_sheet_name
 if target_sheet_name == "IDC":
  select_sql = "select phone_company, record_q, record_percent from phone_statistics where record_company = '%s'" % target_sheet_name
 select_cursor.execute(select_sql, ())

 data_dic = {}
 all_date = {}
 all_phone_company = {}
 for item in select_cursor:
  if target_sheet_name == "IDC":
   data_dic[item[0] + '_' + item[1]] = item[2]
  else:
   if type(item[1]) == str:
    data_dic[item[0] + '_' + item[1]] = item[2]
   else:
    data_dic[item[0] + '_' + item[1].strftime("%Y-%m-%d")] = item[2]
  all_date[item[1]] = 1
  all_phone_company[item[0]] = 1

 if os.path.exists(file_name):
  wb = openpyxl.load_workbook(file_name)
 else:
  wb = Workbook()

 try:
  wb.remove_sheet(wb['Sheet'])
 except Exception as e:
  pass
 try:
  wb.remove_sheet(wb[target_sheet_name])
 except Exception as e:
  pass

 try:
  sheet = wb[target_sheet_name]
 except Exception as e:
  sheet = wb.create_sheet()

 start_date_index = 'B'
 for each_date in all_date.keys():

  if target_sheet_name == "IDC":
   sheet['%s1' % start_date_index] = each_date
  else:
   if type(each_date) == str:
    sheet['%s1' % start_date_index] = each_date
   else:
    sheet['%s1' % start_date_index] = each_date.strftime("%Y-%m-%d")
  start_date_index = chr(ord(start_date_index) + 1)

 start_name_index = 2
 for each_name in all_phone_company.keys():
  sheet['A%d' % start_name_index] = each_name
  start_name_index += 1

 start_date_index = 'B'
 start_name_index = 2
 for each_date in all_date.keys():
  for each_name in all_phone_company.keys():

   if target_sheet_name == "IDC":
    key = each_name + '_' + each_date
    if key in data_dic:
     sheet['%s%d' % (start_date_index, start_name_index)] = data_dic[key]
   else:
    if type(each_date) == str:
     key = each_name + '_' + each_date
    else:
     key = each_name + '_' + each_date.strftime("%Y-%m-%d")
    if key in data_dic:
     sheet['%s%d' % (start_date_index, start_name_index)] = data_dic[key]
   start_name_index += 1
  start_date_index = chr(ord(start_date_index) + 1)
  start_name_index = 2

 sheet.title = target_sheet_name
 sheet.column_dimensions['A'].width = 20
 start_date_index = 'B'
 for each_date in all_date.keys():
  sheet.column_dimensions[start_date_index].width = 13
  start_date_index = chr(ord(start_date_index) + 1)

 add_line_chart(target_sheet_name.upper() + "'s Phone Statistics", sheet, 1, 2, len(all_date.keys()) + 1,
     min(15, len(all_phone_company.keys()) + 1))

 wb.save(file_name)
 pass

补充知识:python plotly line chart 折线图

我就废话不多说了,还是直接看代码吧!

# 1 折线图数据
# trace1 - 基本格式
# trace2 - 更多参数
trace1 = go.Scatter(
 x = x1,
 y = y2,
)
trace2 = go.Scatter(
 x = x2,
 y = y2,
 mode = 'lines', # 模式:lines 线,markers 点。可用“+”相连
 name = 'line2', # 折线名,显示于图例
 connectgaps = True # 连接缺失点两端 默认False
 line = dict(
  color = ('rgb(205, 12, 24)'), # 颜色
  width = 4, #线宽
  dash = 'dash') # 虚线: dash 一一,dot ···,dashdot 一·一
) 
)

# 2 打包数据
data = [trace1,trace2]

# 3 格式
layout = dict(title = '折线',
    xaxis = dict(title = '时间'), # 横轴坐标
    yaxis = dict(title = '数量'), # 总轴坐标
    legend=dict(x=1.1,y=1) # 图例位置 
    )

# 4 打包数据+格式
fig = dict(data=data, layout=layout)

# 5 画图
py.iplot(fig, filename='styled-line')

以上这篇Python openpyxl 插入折线图实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python实现循环语句1到100累和

    python实现循环语句1到100累和

    这篇文章主要介绍了python循环语句1到100累和方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Python分析微信好友性别比例和省份城市分布比例的方法示例【基于itchat模块】

    Python分析微信好友性别比例和省份城市分布比例的方法示例【基于itchat模块】

    这篇文章主要介绍了Python分析微信好友性别比例和省份城市分布比例的方法,结合实例形式分析了Python基于itchat模块获取及计算微信好友相关信息操作技巧,需要的朋友可以参考下
    2020-05-05
  • 详解Python Celery和RabbitMQ实战教程

    详解Python Celery和RabbitMQ实战教程

    这篇文章主要介绍了详解Python Celery和RabbitMQ实战教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Python利用watchdog模块监控文件变化

    Python利用watchdog模块监控文件变化

    这篇文章主要为大家介绍一个Python中的模块:watchdog模块,它可以实现监控文件的变化。文中通过示例详细介绍了watchdog模块的使用,需要的可以参考一下
    2022-06-06
  • Python Pandas 对列/行进行选择,增加,删除操作

    Python Pandas 对列/行进行选择,增加,删除操作

    这篇文章主要介绍了Python Pandas 对列/行进行选择,增加,删除操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Python中requirements.txt简介(推荐)

    Python中requirements.txt简介(推荐)

    Python项目中必须包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号,以便新环境部署,这篇文章主要介绍了Python中requirements.txt简介,需要的朋友可以参考下
    2022-11-11
  • pytorch模型部署到onnx的详细过程

    pytorch模型部署到onnx的详细过程

    这篇文章主要介绍了如何简单的将pytorch模型部署到onnx,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • 基于pytorch中的Sequential用法说明

    基于pytorch中的Sequential用法说明

    这篇文章主要介绍了基于pytorch中的Sequential用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • Python连续赋值需要注意的一些问题

    Python连续赋值需要注意的一些问题

    这篇文章主要介绍了Python连续赋值需要注意的一些问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 常见的在Python中实现单例模式的三种方法

    常见的在Python中实现单例模式的三种方法

    这篇文章主要介绍了常见的在Python中实现单例模式的三种方法,单例模式在各个编程语言的学习中都是需要掌握的基础知识,需要的朋友可以参考下
    2015-04-04

最新评论