99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python批量制作雷達(dá)圖的實(shí)現(xiàn)方法
python批量制作雷達(dá)圖的實(shí)現(xiàn)方法
2018-07-30
收藏

python批量制作雷達(dá)圖的實(shí)現(xiàn)方法

因?yàn)楣ぷ餍枰袝r(shí)候要畫雷達(dá)圖,但是數(shù)據(jù)好多組怎么辦?不能一個(gè)一個(gè)點(diǎn)excel去畫吧,那么可以利用python進(jìn)行批量制作,得到樣式如下:

首先制作一個(gè)演示的excel,評(píng)分為excel隨機(jī)數(shù)生成:

1 =INT((RAND()+4)*10)/10

加入標(biāo)簽等得到的excel樣式如下(部分,共計(jì)32行):


那么接下來就是打開python寫碼了,本文是基于pycharm進(jìn)行編寫
wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\數(shù)據(jù)指標(biāo).xlsx') ##讀取路徑
  ws = wb.get_sheet_by_name("Sheet1") ##讀取名字為Sheet1的sheet表
 
  info_id = []
  info_first = []
 
  for row_A in range(2, 32): ## 遍歷第2行到32行
    id = ws.cell(row=row_A, column=1).value ## 遍歷第2行到32行,第1列
    info_id.append(id)
  for col in range(2, 9): ##讀取第1到9列
    first = ws.cell(row=1, column=col).value
    info_first.append(first) ##得到1到8列的標(biāo)簽
 
  info_data = []
  for row_num_BtoU in range(2, len(info_id) + 2): ## 遍歷第2行到32行
    row_empty = [] ##建立一個(gè)空數(shù)組作為臨時(shí)儲(chǔ)存地,每次換行就被清空
    for i in range(2, 9): ## 遍歷第2行到32行,第2到9列
      data_excel = ws.cell(row=row_num_BtoU, column=i).value
      if data_excel == None:
        pass
      else:
        row_empty.append(data_excel) ##將單元格信息儲(chǔ)存進(jìn)去
    info_data.append(row_empty)

分步講解:

讀取excel表格:    
wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\數(shù)據(jù)指標(biāo).xlsx') ##讀取路徑
ws = wb.get_sheet_by_name("Sheet1") ##讀取名字為Sheet1的sheet表

需要用到庫:

 import xlsxwriter

 from openpyxl import load_workbook

在命令指示符下輸入:

 pip install xlsxwriter

等待安裝即可,后面的庫也是如此:


將第一列ID儲(chǔ)存,以及第一行的標(biāo)簽,標(biāo)簽下面的數(shù)值分別儲(chǔ)存在:

info_id = []
info_first = []
info_data = []

讀取數(shù)據(jù)后接下來需要設(shè)置寫入的格式:    
workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
  worksheet = workbook.add_worksheet() # 創(chuàng)建一個(gè)工作表對(duì)象
  #字體格式
  font = workbook.add_format(
    {'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微軟雅黑'}) ##字體居中,11號(hào),微軟雅黑,給一般的信息用的
  #寫下第一行第一列的標(biāo)簽
  worksheet.write(0, 0, '商品貨號(hào)', font)
  ##設(shè)置圖片的那一列寬度
  worksheet.set_column(0, len(info_first) + 1, 11) # 設(shè)定第len(info_first) + 1列的寬度為11

將標(biāo)簽數(shù)據(jù)等寫入新的excel表格中:    
#新建一個(gè)excel保存結(jié)果
   workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
   worksheet = workbook.add_worksheet() # 創(chuàng)建一個(gè)工作表對(duì)象
   #字體格式
   font = workbook.add_format(
     {'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微軟雅黑'}) ##字體居中,11號(hào),微軟雅黑,給一般的信息用的
   #寫下第一行第一列的標(biāo)簽
   worksheet.write(0, 0, '商品貨號(hào)', font)
   ##設(shè)置圖片的那一列寬度
   worksheet.set_column(0, len(info_first) + 1, 11) # 設(shè)定第len(info_first) + 1列的寬度為11
 
   ##寫入標(biāo)簽
   for k in range(0,7):
     worksheet.write(0, k + 1, info_first[k], font)
   #寫入最后一列標(biāo)簽
   worksheet.write(0, len(info_first) + 1, '雷達(dá)圖', font)

制作雷達(dá)圖:
#設(shè)置雷達(dá)各個(gè)頂點(diǎn)的名稱
labels = np.array(info_first)
#數(shù)據(jù)個(gè)數(shù)
data_len = len(info_first)
for i in range(0,len(info_id)):
  data = np.array(info_data[i])
 
  angles = np.linspace(0, 2*np.pi, data_len, endpoint=False)
  data = np.concatenate((data, [data[0]])) # 閉合
  angles = np.concatenate((angles, [angles[0]])) # 閉合
 
  fig = plt.figure()
  ax = fig.add_subplot(111, polar=True)# polar參數(shù)??!
  ax.plot(angles, data, 'bo-', linewidth=2)# 畫線
  ax.fill(angles, data, facecolor='r', alpha=0.25)# 填充
  ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei")
  ax.set_title("商品貨號(hào):" + str(info_id[i]), va='bottom', fontproperties="SimHei")
  ax.set_rlim(3.8,5)# 設(shè)置雷達(dá)圖的范圍
  ax.grid(True)
  plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品貨號(hào):" + str(info_id[i]) + ".png", dpi=120)

圖片太大怎么辦?用庫改變大小即可:    
import Image
##更改圖片大小
infile = “C:\\Users\\Administrator\\Desktop\\result\\商品貨號(hào):" + str(info_id[i]) + ".png“
outfile = ”C:\\Users\\Administrator\\Desktop\\result1\\商品貨號(hào):" + str(info_id[i]) + ".png”
im = Image.open(infile)
(x, y) = im.size
x_s = 80  ## 設(shè)置長(zhǎng)
y_s = 100  ## 設(shè)置寬
out = im.resize((x_s, y_s), Image.ANTIALIAS)
out.save(outfile,'png',quality = 95)

將大圖片和小圖片放在了result和result1兩個(gè)不同的文件夾,需要再前邊創(chuàng)建這兩個(gè)文件夾:    
if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result'): # 建立一個(gè)文件夾在桌面,文件夾為result
  print('result文件夾已經(jīng)在桌面存在,繼續(xù)運(yùn)行程序……')
else:
  print('result文件夾不在桌面,新建文件夾result')
  os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result')
  print('文件夾建立成功,繼續(xù)運(yùn)行程序')
 
if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result1'): # 建立一個(gè)文件夾在C盤,文件夾為result1
  print('result1文件夾已經(jīng)在桌面存在,繼續(xù)運(yùn)行程序……')
else:
  print('result1文件夾不在桌面,新建文件夾result1')
  os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result1')
  print('文件夾建立成功,繼續(xù)運(yùn)行程序')

最后插入圖片到excel中:    
  worksheet.insert_image(i + 1, len(info_first) + 1, 'C:\\Users\\Administrator\\Desktop\\result1\\' + "商品貨號(hào):" + str(info_id[i]) + '.png') ##寫入圖片
  time.sleep(1)##防止寫入太快電腦死機(jī)
  plt.close() #  一定要關(guān)掉圖片,不然python打開圖片20個(gè)后會(huì)崩潰
 
workbook.close()#最后關(guān)閉excel



得到的效果如下:

附上完整代碼:

import numpy as np
import matplotlib.pyplot as plt
import xlsxwriter
from openpyxl import load_workbook
import os
import time
from PIL import Image
 
if __name__ == '__main__':
 
 if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result'): # 建立一個(gè)文件夾在桌面,文件夾為result
   print('result文件夾已經(jīng)在桌面存在,繼續(xù)運(yùn)行程序……')
 else:
   print('result文件夾不在桌面,新建文件夾result')
   os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result')
   print('文件夾建立成功,繼續(xù)運(yùn)行程序')
 
 if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result1'): # 建立一個(gè)文件夾在C盤,文件夾為result1
   print('result1文件夾已經(jīng)在桌面存在,繼續(xù)運(yùn)行程序……')
 else:
   print('result1文件夾不在桌面,新建文件夾result1')
   os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result1')
   print('文件夾建立成功,繼續(xù)運(yùn)行程序')
 
 wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\數(shù)據(jù)指標(biāo).xlsx') ##讀取路徑
 ws = wb.get_sheet_by_name("Sheet1") ##讀取名字為Sheet1的sheet表
 
 info_id = []
 info_first = []
 
 for row_A in range(2, 32): ## 遍歷第2行到32行
   id = ws.cell(row=row_A, column=1).value ## 遍歷第2行到32行,第1列
   info_id.append(id)
 for col in range(2, 9): ##讀取第1到9列
   first = ws.cell(row=1, column=col).value
   info_first.append(first) ##得到1到8列的標(biāo)簽
 print(info_id)
 print(info_first)
 
 info_data = []
 for row_num_BtoU in range(2, len(info_id) + 2): ## 遍歷第2行到32行
   row_empty = [] ##建立一個(gè)空數(shù)組作為臨時(shí)儲(chǔ)存地,每次換行就被清空
   for i in range(2, 9): ## 遍歷第2行到32行,第2到9列
     data_excel = ws.cell(row=row_num_BtoU, column=i).value
     if data_excel == None:
       pass
     else:
       row_empty.append(data_excel) ##將單元格信息儲(chǔ)存進(jìn)去
   info_data.append(row_empty)
 print(info_data)
 print(len(info_data))
 
 # 設(shè)置雷達(dá)各個(gè)頂點(diǎn)的名稱
 labels = np.array(info_first)
 # 數(shù)據(jù)個(gè)數(shù)
 data_len = len(info_first)
 # 新建一個(gè)excel保存結(jié)果
 workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
 worksheet = workbook.add_worksheet() # 創(chuàng)建一個(gè)工作表對(duì)象
 # 字體格式
 font = workbook.add_format(
   {'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微軟雅黑'}) ##字體居中,11號(hào),微軟雅黑,給一般的信息用的
 # 寫下第一行第一列的標(biāo)簽
 worksheet.write(0, 0, '商品貨號(hào)', font)
 ##設(shè)置圖片的那一列寬度
 worksheet.set_column(0, len(info_first) + 1, 11) # 設(shè)定第len(info_first) + 1列的寬度為11
 
 ##寫入標(biāo)簽
 for k in range(0, 7):
   worksheet.write(0, k + 1, info_first[k], font)
 # 寫入最后一列標(biāo)簽
 worksheet.write(0, len(info_first) + 1, '雷達(dá)圖', font)
 
 # 將其他參數(shù)寫入excel中
 for j in range(0, len(info_id)):
   worksheet.write(j + 1, 0, info_id[j], font) # 寫入商品貨號(hào)
   worksheet.set_row(j, 76) ##設(shè)置行寬
   for x in range(0, len(info_first)):
     worksheet.write(j + 1, x + 1, info_data[j][x], font) # 寫入商品的其他參數(shù)
 
 for i in range(0, len(info_id)):
   data = np.array(info_data[i])
 
   angles = np.linspace(0, 2 * np.pi, data_len, endpoint=False)
   data = np.concatenate((data, [data[0]])) # 閉合
   angles = np.concatenate((angles, [angles[0]])) # 閉合
 
   fig = plt.figure()
   ax = fig.add_subplot(111, polar=True) # polar參數(shù)?。?br />    ax.plot(angles, data, 'bo-', linewidth=2) # 畫線
   ax.fill(angles, data, facecolor='r', alpha=0.25) # 填充
   ax.set_thetagrids(angles * 180 / np.pi, labels, fontproperties="SimHei")
   ax.set_title("商品貨號(hào):" + str(info_id[i]), va='bottom', fontproperties="SimHei")
   ax.set_rlim(3.8, 5) # 設(shè)置雷達(dá)圖的范圍
   ax.grid(True)
   plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品貨號(hào):" + str(info_id[i]) + ".png", dpi=120)
   # plt.show()在python中顯示
 
   ##更改圖片大小
   infile = "C:\\Users\\Administrator\\Desktop\\result\\商品貨號(hào):" + str(info_id[i]) + ".png"
   outfile = "C:\\Users\\Administrator\\Desktop\\result1\\商品貨號(hào):" + str(info_id[i]) + ".png"
   im = Image.open(infile)
   (x, y) = im.size
   x_s = 80 ## 設(shè)置長(zhǎng)
   y_s = 100 ## 設(shè)置寬
   out = im.resize((x_s, y_s), Image.ANTIALIAS)
   out.save(outfile, 'png', quality=95)
 
   worksheet.insert_image(i + 1, len(info_first) + 1,
              'C:\\Users\\Administrator\\Desktop\\result1\\' + "商品貨號(hào):" + str(
                info_id[i]) + '.png') ##寫入圖片
   time.sleep(1) ##防止寫入太快電腦死機(jī)
   plt.close() # 一定要關(guān)掉圖片,不然python打開圖片20個(gè)后會(huì)崩潰
 
 workbook.close() # 最后關(guān)閉excel
以上就是本文介紹利用python批量制作雷達(dá)圖的實(shí)現(xiàn)方法,希望給學(xué)習(xí)python的大家有所幫助


數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }