最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python openpyxl 插入折線圖實(shí)例

 更新時(shí)間:2020年04月17日 14:34:02   作者:瓦力冫  
這篇文章主要介紹了Python openpyxl 插入折線圖實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,還是直接看代碼吧!

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 # 圖的標(biāo)題
 c1.style = 12 # 線條的style
 c1.y_axis.title = 'percent' # y坐標(biāo)的標(biāo)題
 if 'IDC' not in title:
  c1.x_axis.number_format = 'd-mmm' # 規(guī)定日期格式 這是月,年格式
  c1.x_axis.majorTimeUnit = "Months" # 規(guī)定日期間隔 注意days;Months大寫
 c1.x_axis.title = "Date" # x坐標(biāo)的標(biāo)題
 data = Reference(wss, min_col=min_col, min_row=min_row, max_col=max_col, max_row=max_row) # 圖像的數(shù)據(jù) 起始行、起始列、終止行、終止列
 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

補(bǔ)充知識(shí):python plotly line chart 折線圖

我就廢話不多說了,還是直接看代碼吧!

# 1 折線圖數(shù)據(jù)
# trace1 - 基本格式
# trace2 - 更多參數(shù)
trace1 = go.Scatter(
 x = x1,
 y = y2,
)
trace2 = go.Scatter(
 x = x2,
 y = y2,
 mode = 'lines', # 模式:lines 線,markers 點(diǎn)??捎谩?”相連
 name = 'line2', # 折線名,顯示于圖例
 connectgaps = True # 連接缺失點(diǎn)兩端 默認(rèn)False
 line = dict(
  color = ('rgb(205, 12, 24)'), # 顏色
  width = 4, #線寬
  dash = 'dash') # 虛線: dash 一一,dot ···,dashdot 一·一
) 
)

# 2 打包數(shù)據(jù)
data = [trace1,trace2]

# 3 格式
layout = dict(title = '折線',
    xaxis = dict(title = '時(shí)間'), # 橫軸坐標(biāo)
    yaxis = dict(title = '數(shù)量'), # 總軸坐標(biāo)
    legend=dict(x=1.1,y=1) # 圖例位置 
    )

# 4 打包數(shù)據(jù)+格式
fig = dict(data=data, layout=layout)

# 5 畫圖
py.iplot(fig, filename='styled-line')

以上這篇Python openpyxl 插入折線圖實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python實(shí)現(xiàn)循環(huán)語句1到100累和

    python實(shí)現(xiàn)循環(huán)語句1到100累和

    這篇文章主要介紹了python循環(huán)語句1到100累和方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python分析微信好友性別比例和省份城市分布比例的方法示例【基于itchat模塊】

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

    這篇文章主要介紹了Python分析微信好友性別比例和省份城市分布比例的方法,結(jié)合實(shí)例形式分析了Python基于itchat模塊獲取及計(jì)算微信好友相關(guān)信息操作技巧,需要的朋友可以參考下
    2020-05-05
  • 詳解Python Celery和RabbitMQ實(shí)戰(zhàn)教程

    詳解Python Celery和RabbitMQ實(shí)戰(zhàn)教程

    這篇文章主要介紹了詳解Python Celery和RabbitMQ實(shí)戰(zhàn)教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python利用watchdog模塊監(jiān)控文件變化

    Python利用watchdog模塊監(jiān)控文件變化

    這篇文章主要為大家介紹一個(gè)Python中的模塊:watchdog模塊,它可以實(shí)現(xiàn)監(jiān)控文件的變化。文中通過示例詳細(xì)介紹了watchdog模塊的使用,需要的可以參考一下
    2022-06-06
  • Python Pandas 對(duì)列/行進(jìn)行選擇,增加,刪除操作

    Python Pandas 對(duì)列/行進(jìn)行選擇,增加,刪除操作

    這篇文章主要介紹了Python Pandas 對(duì)列/行進(jìn)行選擇,增加,刪除操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Python中requirements.txt簡介(推薦)

    Python中requirements.txt簡介(推薦)

    Python項(xiàng)目中必須包含一個(gè)?requirements.txt?文件,用于記錄所有依賴包及其精確的版本號(hào),以便新環(huán)境部署,這篇文章主要介紹了Python中requirements.txt簡介,需要的朋友可以參考下
    2022-11-11
  • pytorch模型部署到onnx的詳細(xì)過程

    pytorch模型部署到onnx的詳細(xì)過程

    這篇文章主要介紹了如何簡單的將pytorch模型部署到onnx,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 基于pytorch中的Sequential用法說明

    基于pytorch中的Sequential用法說明

    這篇文章主要介紹了基于pytorch中的Sequential用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python連續(xù)賦值需要注意的一些問題

    Python連續(xù)賦值需要注意的一些問題

    這篇文章主要介紹了Python連續(xù)賦值需要注意的一些問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 常見的在Python中實(shí)現(xiàn)單例模式的三種方法

    常見的在Python中實(shí)現(xiàn)單例模式的三種方法

    這篇文章主要介紹了常見的在Python中實(shí)現(xiàn)單例模式的三種方法,單例模式在各個(gè)編程語言的學(xué)習(xí)中都是需要掌握的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-04-04

最新評(píng)論

行唐县| 云和县| 封开县| 克山县| 柳州市| 贵德县| 甘肃省| 平顺县| 九龙县| 合阳县| 西林县| 商都县| 万安县| 玛多县| 北流市| 桐庐县| 密山市| 青河县| 蒲城县| 获嘉县| 连江县| 中超| 车险| 宜川县| 定南县| 嵊州市| 咸阳市| 古田县| 东明县| 邢台市| 苍溪县| 融水| 潜江市| 阿克苏市| 广水市| 寿阳县| 蒙山县| 元氏县| 桂平市| 静宁县| 华阴市|