老板要畫雷達圖,但是數據好多組怎么辦?不能一個一個點excel去畫吧,那么可以利用python進行批量制作,得到樣式如下:
首先制作一個演示的excel,評分為excel隨機數生成:
1 =INT((RAND()+4)*10)/10
加入標簽等得到的excel樣式如下(部分,共計32行):
那么接下來就是打開python寫碼了,本文是基于python3.4進行編寫
1 wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\數據指標.xlsx')? ##讀取路徑
2? ? ?ws = wb.get_sheet_by_name("Sheet1")? ##讀取名字為Sheet1的sheet表
3
4? ? ?info_id = []
5? ? ?info_first = []
6
7? ? ?for row_A in range(2, 32):? ## 遍歷第2行到32行
8? ? ? ? ?id = ws.cell(row=row_A, column=1).value? ## 遍歷第2行到32行,第1列
9? ? ? ? ?info_id.append(id)
10? ? ?for col in range(2, 9):? ##讀取第1到9列
11? ? ? ? ?first = ws.cell(row=1, column=col).value
12? ? ? ? ?info_first.append(first)? ##得到1到8列的標簽
13
14? ? ?info_data = []
15? ? ?for row_num_BtoU in range(2, len(info_id) + 2):? ## 遍歷第2行到32行
16? ? ? ? ?row_empty = []? ##建立一個空數組作為臨時儲存地,每次換行就被清空
17? ? ? ? ?for i in range(2, 9):? ## 遍歷第2行到32行,第2到9列
18? ? ? ? ? ? ?data_excel = ws.cell(row=row_num_BtoU, column=i).value
19? ? ? ? ? ? ?if data_excel == None:
20? ? ? ? ? ? ? ? ?pass
21? ? ? ? ? ? ?else:
22? ? ? ? ? ? ? ? ?row_empty.append(data_excel)? ##將單元格信息儲存進去
23? ? ? ? ?info_data.append(row_empty)
分步講解:
讀取excel表格:
1 wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\數據指標.xlsx')? ##讀取路徑
2? ? ?ws = wb.get_sheet_by_name("Sheet1")? ##讀取名字為Sheet1的sheet表
需要用到庫:
1 import xlsxwriter
1 from openpyxl import load_workbook
在命令指示符下輸入:
1 pip install xlsxwriter
等待安裝即可,后面的庫也是如此:
將第一列ID儲存,以及第一行的標簽,標簽下面的數值分別儲存在:
info_id = []
info_first = []
info_data = []
讀取數據后接下來需要設置寫入的格式:
1 workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
2? ? ?worksheet = workbook.add_worksheet()? # 創建一個工作表對象
3? ? ?#字體格式
4? ? ?font = workbook.add_format(
5? ? ? ? ?{'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微軟雅黑'})? ##字體居中,11號,微軟雅黑,給一般的信息用的
6? ? ?#寫下第一行第一列的標簽
7? ? ?worksheet.write(0, 0, '商品貨號', font)
8? ? ?##設置圖片的那一列寬度
9? ? ?worksheet.set_column(0, len(info_first) + 1, 11)? # 設定第len(info_first) + 1列的寬度為11
將標簽數據等寫入新的excel表格中:
1 #新建一個excel保存結果
2? ? ?workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
3? ? ?worksheet = workbook.add_worksheet()? # 創建一個工作表對象
4? ? ?#字體格式
5? ? ?font = workbook.add_format(
6? ? ? ? ?{'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微軟雅黑'})? ##字體居中,11號,微軟雅黑,給一般的信息用的
7? ? ?#寫下第一行第一列的標簽
8? ? ?worksheet.write(0, 0, '商品貨號', font)
9? ? ?##設置圖片的那一列寬度
10? ? ?worksheet.set_column(0, len(info_first) + 1, 11)? # 設定第len(info_first) + 1列的寬度為11
11
12? ? ?##寫入標簽
13? ? ?for k in range(0,7):
14? ? ? ? ?worksheet.write(0, k + 1, info_first[k], font)
15? ? ?#寫入最后一列標簽
16? ? ?worksheet.write(0, len(info_first) + 1, '雷達圖', font)
制作雷達圖:
1? ? ?#設置雷達各個頂點的名稱
2? ? ?labels = np.array(info_first)
3? ? ?#數據個數
4? ? ?data_len = len(info_first)
5? ? ?for i in range(0,len(info_id)):
6? ? ? ? ?data = np.array(info_data[i])
7
8? ? ? ? ?angles = np.linspace(0, 2*np.pi, data_len, endpoint=False)
9? ? ? ? ?data = np.concatenate((data, [data[0]])) # 閉合
10? ? ? ? ?angles = np.concatenate((angles, [angles[0]])) # 閉合
11
12? ? ? ? ?fig = plt.figure()
13? ? ? ? ?ax = fig.add_subplot(111, polar=True)# polar參數!!
14? ? ? ? ?ax.plot(angles, data, 'bo-', linewidth=2)# 畫線
15? ? ? ? ?ax.fill(angles, data, facecolor='r', alpha=0.25)# 填充
16? ? ? ? ?ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei")
17? ? ? ? ?ax.set_title("商品貨號:" + str(info_id[i]), va='bottom', fontproperties="SimHei")
18? ? ? ? ?ax.set_rlim(3.8,5)# 設置雷達圖的范圍
19? ? ? ? ?ax.grid(True)
20? ? ? ? ?plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品貨號:" + str(info_id[i]) + ".png", dpi=120)
圖片太大怎么辦?用庫改變大小即可:
1? ? ? ? ?import Image
2? ? ? ? ?##更改圖片大小
3? ? ? ? ?infile = “C:\\Users\\Administrator\\Desktop\\result\\商品貨號:" + str(info_id[i]) + ".png“
4? ? ? ? ?outfile = ”C:\\Users\\Administrator\\Desktop\\result1\\商品貨號:" + str(info_id[i]) + ".png”
5? ? ? ? ?im = Image.open(infile)
6? ? ? ? ?(x, y) = im.size
7? ? ? ? ?x_s = 80? ? ## 設置長
8? ? ? ? ?y_s = 100 ## 設置寬
9? ? ? ? ?out = im.resize((x_s, y_s), Image.ANTIALIAS)
10? ? ? ? ?out.save(outfile,'png',quality = 95)
將大圖片和小圖片放在了result和result1兩個不同的文件夾,需要再前邊創建這兩個文件夾:
1? ? ?if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result'):? # 建立一個文件夾在桌面,文件夾為result
2? ? ? ? ?print('result文件夾已經在桌面存在,繼續運行程序……')
3? ? ?else:
4? ? ? ? ?print('result文件夾不在桌面,新建文件夾result')
5? ? ? ? ?os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result')
6? ? ? ? ?print('文件夾建立成功,繼續運行程序')
7
8? ? ?if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result1'):? # 建立一個文件夾在C盤,文件夾為result1
9? ? ? ? ?print('result1文件夾已經在桌面存在,繼續運行程序……')
10? ? ?else:
11? ? ? ? ?print('result1文件夾不在桌面,新建文件夾result1')
12? ? ? ? ?os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result1')
13? ? ? ? ?print('文件夾建立成功,繼續運行程序')
最后插入圖片到excel中:
1? ? ? ? ?worksheet.insert_image(i + 1, len(info_first) + 1, 'C:\\Users\\Administrator\\Desktop\\result1\\' + "商品貨號:" + str(info_id[i]) + '.png')? ##寫入圖片
2? ? ? ? ?time.sleep(1)##防止寫入太快電腦死機
3? ? ? ? ?plt.close() #? ?一定要關掉圖片,不然python打開圖片20個后會崩潰
4
5? ? ?workbook.close()#最后關閉excel
得到的效果如下:
附上完整代碼:
1 import numpy as np
2 import matplotlib.pyplot as plt
3 import xlsxwriter
4 from openpyxl import load_workbook
5 import os
6 import time
7 from PIL import Image
8
9 if __name__ == '__main__':
10
11? ? ?if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result'):? # 建立一個文件夾在桌面,文件夾為result
12? ? ? ? ?print('result文件夾已經在桌面存在,繼續運行程序……')
13? ? ?else:
14? ? ? ? ?print('result文件夾不在桌面,新建文件夾result')
15? ? ? ? ?os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result')
16? ? ? ? ?print('文件夾建立成功,繼續運行程序')
17
18? ? ?if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result1'):? # 建立一個文件夾在C盤,文件夾為result1
19? ? ? ? ?print('result1文件夾已經在桌面存在,繼續運行程序……')
20? ? ?else:
21? ? ? ? ?print('result1文件夾不在桌面,新建文件夾result1')
22? ? ? ? ?os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result1')
23? ? ? ? ?print('文件夾建立成功,繼續運行程序')
24
25? ? ?wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\數據指標.xlsx')? ##讀取路徑
26? ? ?ws = wb.get_sheet_by_name("Sheet1")? ##讀取名字為Sheet1的sheet表
27
28? ? ?info_id = []
29? ? ?info_first = []
30
31? ? ?for row_A in range(2, 32):? ## 遍歷第2行到32行
32? ? ? ? ?id = ws.cell(row=row_A, column=1).value? ## 遍歷第2行到32行,第1列
33? ? ? ? ?info_id.append(id)
34? ? ?for col in range(2, 9):? ##讀取第1到9列
35? ? ? ? ?first = ws.cell(row=1, column=col).value
36? ? ? ? ?info_first.append(first)? ##得到1到8列的標簽
37? ? ?print(info_id)
38? ? ?print(info_first)
39
40? ? ?info_data = []
41? ? ?for row_num_BtoU in range(2, len(info_id) + 2):? ## 遍歷第2行到32行
42? ? ? ? ?row_empty = []? ##建立一個空數組作為臨時儲存地,每次換行就被清空
43? ? ? ? ?for i in range(2, 9):? ## 遍歷第2行到32行,第2到9列
44? ? ? ? ? ? ?data_excel = ws.cell(row=row_num_BtoU, column=i).value
45? ? ? ? ? ? ?if data_excel == None:
46? ? ? ? ? ? ? ? ?pass
47? ? ? ? ? ? ?else:
48? ? ? ? ? ? ? ? ?row_empty.append(data_excel)? ##將單元格信息儲存進去
49? ? ? ? ?info_data.append(row_empty)
50? ? ?print(info_data)
51? ? ?print(len(info_data))
52
53? ? ?# 設置雷達各個頂點的名稱
54? ? ?labels = np.array(info_first)
55? ? ?# 數據個數
56? ? ?data_len = len(info_first)
57? ? ?# 新建一個excel保存結果
58? ? ?workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
59? ? ?worksheet = workbook.add_worksheet()? # 創建一個工作表對象
60? ? ?# 字體格式
61? ? ?font = workbook.add_format(
62? ? ? ? ?{'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微軟雅黑'})? ##字體居中,11號,微軟雅黑,給一般的信息用的
63? ? ?# 寫下第一行第一列的標簽
64? ? ?worksheet.write(0, 0, '商品貨號', font)
65? ? ?##設置圖片的那一列寬度
66? ? ?worksheet.set_column(0, len(info_first) + 1, 11)? # 設定第len(info_first) + 1列的寬度為11
67
68? ? ?##寫入標簽
69? ? ?for k in range(0, 7):
70? ? ? ? ?worksheet.write(0, k + 1, info_first[k], font)
71? ? ?# 寫入最后一列標簽
72? ? ?worksheet.write(0, len(info_first) + 1, '雷達圖', font)
73
74? ? ?# 將其他參數寫入excel中
75? ? ?for j in range(0, len(info_id)):
76? ? ? ? ?worksheet.write(j + 1, 0, info_id[j], font)? # 寫入商品貨號
77? ? ? ? ?worksheet.set_row(j, 76)? ##設置行寬
78? ? ? ? ?for x in range(0, len(info_first)):
79? ? ? ? ? ? ?worksheet.write(j + 1, x + 1, info_data[j][x], font)? # 寫入商品的其他參數
80
81? ? ?for i in range(0, len(info_id)):
82? ? ? ? ?data = np.array(info_data[i])
83
84? ? ? ? ?angles = np.linspace(0, 2 * np.pi, data_len, endpoint=False)
85? ? ? ? ?data = np.concatenate((data, [data[0]]))? # 閉合
86? ? ? ? ?angles = np.concatenate((angles, [angles[0]]))? # 閉合
87
88? ? ? ? ?fig = plt.figure()
89? ? ? ? ?ax = fig.add_subplot(111, polar=True)? # polar參數!!
90? ? ? ? ?ax.plot(angles, data, 'bo-', linewidth=2)? # 畫線
91? ? ? ? ?ax.fill(angles, data, facecolor='r', alpha=0.25)? # 填充
92? ? ? ? ?ax.set_thetagrids(angles * 180 / np.pi, labels, fontproperties="SimHei")
93? ? ? ? ?ax.set_title("商品貨號:" + str(info_id[i]), va='bottom', fontproperties="SimHei")
94? ? ? ? ?ax.set_rlim(3.8, 5)? # 設置雷達圖的范圍
95? ? ? ? ?ax.grid(True)
96? ? ? ? ?plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品貨號:" + str(info_id[i]) + ".png", dpi=120)
97? ? ? ? ?# plt.show()在python中顯示
98
99? ? ? ? ?##更改圖片大小
100? ? ? ? ?infile = "C:\\Users\\Administrator\\Desktop\\result\\商品貨號:" + str(info_id[i]) + ".png"
101? ? ? ? ?outfile = "C:\\Users\\Administrator\\Desktop\\result1\\商品貨號:" + str(info_id[i]) + ".png"
102? ? ? ? ?im = Image.open(infile)
103? ? ? ? ?(x, y) = im.size
104? ? ? ? ?x_s = 80? ## 設置長
105? ? ? ? ?y_s = 100? ## 設置寬
106? ? ? ? ?out = im.resize((x_s, y_s), Image.ANTIALIAS)
107? ? ? ? ?out.save(outfile, 'png', quality=95)
108
109? ? ? ? ?worksheet.insert_image(i + 1, len(info_first) + 1,
110? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'C:\\Users\\Administrator\\Desktop\\result1\\' + "商品貨號:" + str(
111? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? info_id[i]) + '.png')? ##寫入圖片
112? ? ? ? ?time.sleep(1)? ##防止寫入太快電腦死機
113? ? ? ? ?plt.close()? # 一定要關掉圖片,不然python打開圖片20個后會崩潰
114
115? ? ?workbook.close()? # 最后關閉excel
轉載于:https://www.cnblogs.com/TTyb/p/5703106.html