
python批量制作雷达图的实现方法
因为工作需要有时候要画雷达图,但是数据好多组怎么办?不能一个一个点excel去画吧,那么可以利用python进行批量制作,得到样式如下:
首先制作一个演示的excel,评分为excel随机数生成:
1 =INT((RAND()+4)*10)/10
加入标签等得到的excel样式如下(部分,共计32行):
那么接下来就是打开python写码了,本文是基于pycharm进行编写
wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\数据指标.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列的标签
info_data = []
for row_num_BtoU in range(2, len(info_id) + 2): ## 遍历第2行到32行
row_empty = [] ##建立一个空数组作为临时储存地,每次换行就被清空
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) ##将单元格信息储存进去
info_data.append(row_empty)
分步讲解:
读取excel表格:
wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\数据指标.xlsx') ##读取路径
ws = wb.get_sheet_by_name("Sheet1") ##读取名字为Sheet1的sheet表
需要用到库:
import xlsxwriter
from openpyxl import load_workbook
在命令指示符下输入:
pip install xlsxwriter
等待安装即可,后面的库也是如此:
将第一列ID储存,以及第一行的标签,标签下面的数值分别储存在:
info_id = []
info_first = []
info_data = []
读取数据后接下来需要设置写入的格式:
workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
worksheet = workbook.add_worksheet() # 创建一个工作表对象
#字体格式
font = workbook.add_format(
{'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微软雅黑'}) ##字体居中,11号,微软雅黑,给一般的信息用的
#写下第一行第一列的标签
worksheet.write(0, 0, '商品货号', font)
##设置图片的那一列宽度
worksheet.set_column(0, len(info_first) + 1, 11) # 设定第len(info_first) + 1列的宽度为11
将标签数据等写入新的excel表格中:
#新建一个excel保存结果
workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
worksheet = workbook.add_worksheet() # 创建一个工作表对象
#字体格式
font = workbook.add_format(
{'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微软雅黑'}) ##字体居中,11号,微软雅黑,给一般的信息用的
#写下第一行第一列的标签
worksheet.write(0, 0, '商品货号', font)
##设置图片的那一列宽度
worksheet.set_column(0, len(info_first) + 1, 11) # 设定第len(info_first) + 1列的宽度为11
##写入标签
for k in range(0,7):
worksheet.write(0, k + 1, info_first[k], font)
#写入最后一列标签
worksheet.write(0, len(info_first) + 1, '雷达图', font)
制作雷达图:
#设置雷达各个顶点的名称
labels = np.array(info_first)
#数据个数
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参数!!
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("商品货号:" + str(info_id[i]), va='bottom', fontproperties="SimHei")
ax.set_rlim(3.8,5)# 设置雷达图的范围
ax.grid(True)
plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品货号:" + str(info_id[i]) + ".png", dpi=120)
图片太大怎么办?用库改变大小即可:
import Image
##更改图片大小
infile = “C:\\Users\\Administrator\\Desktop\\result\\商品货号:" + str(info_id[i]) + ".png“
outfile = ”C:\\Users\\Administrator\\Desktop\\result1\\商品货号:" + str(info_id[i]) + ".png”
im = Image.open(infile)
(x, y) = im.size
x_s = 80 ## 设置长
y_s = 100 ## 设置宽
out = im.resize((x_s, y_s), Image.ANTIALIAS)
out.save(outfile,'png',quality = 95)
将大图片和小图片放在了result和result1两个不同的文件夹,需要再前边创建这两个文件夹:
if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result'): # 建立一个文件夹在桌面,文件夹为result
print('result文件夹已经在桌面存在,继续运行程序……')
else:
print('result文件夹不在桌面,新建文件夹result')
os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result')
print('文件夹建立成功,继续运行程序')
if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result1'): # 建立一个文件夹在C盘,文件夹为result1
print('result1文件夹已经在桌面存在,继续运行程序……')
else:
print('result1文件夹不在桌面,新建文件夹result1')
os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result1')
print('文件夹建立成功,继续运行程序')
最后插入图片到excel中:
worksheet.insert_image(i + 1, len(info_first) + 1,
'C:\\Users\\Administrator\\Desktop\\result1\\' + "商品货号:" +
str(info_id[i]) + '.png') ##写入图片
time.sleep(1)##防止写入太快电脑死机
plt.close() # 一定要关掉图片,不然python打开图片20个后会崩溃
workbook.close()#最后关闭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'): # 建立一个文件夹在桌面,文件夹为result
print('result文件夹已经在桌面存在,继续运行程序……')
else:
print('result文件夹不在桌面,新建文件夹result')
os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result')
print('文件夹建立成功,继续运行程序')
if os.path.exists(r'C:\\Users\\Administrator\\Desktop\\result1'): # 建立一个文件夹在C盘,文件夹为result1
print('result1文件夹已经在桌面存在,继续运行程序……')
else:
print('result1文件夹不在桌面,新建文件夹result1')
os.mkdir(r'C:\\Users\\Administrator\\Desktop\\result1')
print('文件夹建立成功,继续运行程序')
wb = load_workbook(filename=r'C:\Users\Administrator\Desktop\数据指标.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列的标签
print(info_id)
print(info_first)
info_data = []
for row_num_BtoU in range(2, len(info_id) + 2): ## 遍历第2行到32行
row_empty = [] ##建立一个空数组作为临时储存地,每次换行就被清空
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) ##将单元格信息储存进去
info_data.append(row_empty)
print(info_data)
print(len(info_data))
# 设置雷达各个顶点的名称
labels = np.array(info_first)
# 数据个数
data_len = len(info_first)
# 新建一个excel保存结果
workbook = xlsxwriter.Workbook('C:\\Users\\Administrator\\Desktop\\result.xlsx')
worksheet = workbook.add_worksheet() # 创建一个工作表对象
# 字体格式
font = workbook.add_format(
{'border': 1, 'align': 'center', 'font_size': 11, 'font_name': '微软雅黑'}) ##字体居中,11号,微软雅黑,给一般的信息用的
# 写下第一行第一列的标签
worksheet.write(0, 0, '商品货号', font)
##设置图片的那一列宽度
worksheet.set_column(0, len(info_first) + 1, 11) # 设定第len(info_first) + 1列的宽度为11
##写入标签
for k in range(0, 7):
worksheet.write(0, k + 1, info_first[k], font)
# 写入最后一列标签
worksheet.write(0, len(info_first) + 1, '雷达图', font)
# 将其他参数写入excel中
for j in range(0, len(info_id)):
worksheet.write(j + 1, 0, info_id[j], font) # 写入商品货号
worksheet.set_row(j, 76) ##设置行宽
for x in range(0, len(info_first)):
worksheet.write(j + 1, x + 1, info_data[j][x], font) # 写入商品的其他参数
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参数!!
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("商品货号:" + str(info_id[i]), va='bottom', fontproperties="SimHei")
ax.set_rlim(3.8, 5) # 设置雷达图的范围
ax.grid(True)
plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品货号:" + str(info_id[i]) + ".png", dpi=120)
# plt.show()在python中显示
##更改图片大小
infile = "C:\\Users\\Administrator\\Desktop\\result\\商品货号:" + str(info_id[i]) + ".png"
outfile = "C:\\Users\\Administrator\\Desktop\\result1\\商品货号:" + str(info_id[i]) + ".png"
im = Image.open(infile)
(x, y) = im.size
x_s = 80 ## 设置长
y_s = 100 ## 设置宽
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\\' + "商品货号:" + str(
info_id[i]) + '.png') ##写入图片
time.sleep(1) ##防止写入太快电脑死机
plt.close() # 一定要关掉图片,不然python打开图片20个后会崩溃
workbook.close() # 最后关闭excel
以上就是本文介绍利用python批量制作雷达图的实现方法,希望给学习python的大家有所帮助
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
在 “神经网络与卡尔曼滤波融合” 的理论基础上,Python 凭借其丰富的科学计算库(NumPy、FilterPy)、深度学习框架(PyTorch、T ...
2025-10-23在工业控制、自动驾驶、机器人导航、气象预测等领域,“状态估计” 是核心任务 —— 即从含噪声的观测数据中,精准推断系统的真 ...
2025-10-23在数据分析全流程中,“数据清洗” 恰似烹饪前的食材处理:若食材(数据)腐烂变质、混杂异物(脏数据),即便拥有精湛的烹饪技 ...
2025-10-23在人工智能领域,“大模型” 已成为近年来的热点标签:从参数超 1750 亿的 GPT-3,到万亿级参数的 PaLM,再到多模态大模型 GPT-4 ...
2025-10-22在 MySQL 数据库的日常运维与开发中,“更新数据是否会影响读数据” 是一个高频疑问。这个问题的答案并非简单的 “是” 或 “否 ...
2025-10-22在企业数据分析中,“数据孤岛” 是制约分析深度的核心瓶颈 —— 用户数据散落在注册系统、APP 日志、客服记录中,订单数据分散 ...
2025-10-22在神经网络设计中,“隐藏层个数” 是决定模型能力的关键参数 —— 太少会导致 “欠拟合”(模型无法捕捉复杂数据规律,如用单隐 ...
2025-10-21在特征工程流程中,“单变量筛选” 是承上启下的关键步骤 —— 它通过分析单个特征与目标变量的关联强度,剔除无意义、冗余的特 ...
2025-10-21在数据分析全流程中,“数据读取” 常被误解为 “简单的文件打开”—— 双击 Excel、执行基础 SQL 查询即可完成。但对 CDA(Cert ...
2025-10-21在实际业务数据分析中,我们遇到的大多数数据并非理想的正态分布 —— 电商平台的用户消费金额(少数用户单次消费上万元,多数集 ...
2025-10-20在数字化交互中,用户的每一次操作 —— 从电商平台的 “浏览商品→加入购物车→查看评价→放弃下单”,到内容 APP 的 “点击短 ...
2025-10-20在数据分析的全流程中,“数据采集” 是最基础也最关键的环节 —— 如同烹饪前需备好新鲜食材,若采集的数据不完整、不准确或不 ...
2025-10-20在数据成为新时代“石油”的今天,几乎每个职场人都在焦虑: “为什么别人能用数据驱动决策、升职加薪,而我面对Excel表格却无从 ...
2025-10-18数据清洗是 “数据价值挖掘的前置关卡”—— 其核心目标是 “去除噪声、修正错误、规范格式”,但前提是不破坏数据的真实业务含 ...
2025-10-17在数据汇总分析中,透视表凭借灵活的字段重组能力成为核心工具,但原始透视表仅能呈现数值结果,缺乏对数据背景、异常原因或业务 ...
2025-10-17在企业管理中,“凭经验定策略” 的传统模式正逐渐失效 —— 金融机构靠 “研究员主观判断” 选股可能错失收益,电商靠 “运营拍 ...
2025-10-17在数据库日常操作中,INSERT INTO SELECT是实现 “批量数据迁移” 的核心 SQL 语句 —— 它能直接将一个表(或查询结果集)的数 ...
2025-10-16在机器学习建模中,“参数” 是决定模型效果的关键变量 —— 无论是线性回归的系数、随机森林的树深度,还是神经网络的权重,这 ...
2025-10-16在数字化浪潮中,“数据” 已从 “辅助决策的工具” 升级为 “驱动业务的核心资产”—— 电商平台靠用户行为数据优化推荐算法, ...
2025-10-16在大模型从实验室走向生产环境的过程中,“稳定性” 是决定其能否实用的关键 —— 一个在单轮测试中表现优异的模型,若在高并发 ...
2025-10-15