中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python中如何使用openpyxl插入折線圖實例

發布時間:2020-10-27 09:30:00 來源:億速云 閱讀:822 作者:小新 欄目:編程語言

小編給大家分享一下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插入折線圖實例有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

甘孜县| 五大连池市| 米林县| 嵩明县| 班玛县| 昌图县| 卢湾区| 临猗县| 泸定县| 宁津县| 河东区| 教育| 台江县| 井冈山市| 拜泉县| 南部县| 体育| 望江县| 雷波县| 喀喇沁旗| 平塘县| 囊谦县| 建瓯市| 盈江县| 凌源市| 平顶山市| 博白县| 巴南区| 特克斯县| 星子县| 东乌珠穆沁旗| 和静县| 石柱| 昭觉县| 始兴县| 南郑县| 汨罗市| 鹤壁市| 本溪市| 南木林县| 丰都县|