京公网安备 11010802034615号
经营许可证编号:京B2-20210330
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
在用户行为分析实践中,很多从业者会陷入一个核心误区:过度关注“当前数据的分析结果”,却忽视了结果的“泛化能力”——即分析 ...
2026-03-13在数字经济时代,用户的每一次点击、浏览、停留、转化,都在传递着真实的需求信号。用户行为分析,本质上是通过收集、整理、挖掘 ...
2026-03-13在金融、零售、互联网等数据密集型行业,量化策略已成为企业挖掘商业价值、提升决策效率、控制经营风险的核心工具。而CDA(Certi ...
2026-03-13在机器学习建模体系中,随机森林作为集成学习的经典算法,凭借高精度、抗过拟合、适配多场景、可解释性强的核心优势,成为分类、 ...
2026-03-12在机器学习建模过程中,“哪些特征对预测结果影响最大?”“如何筛选核心特征、剔除冗余信息?”是从业者最常面临的核心问题。随 ...
2026-03-12在数字化转型深度渗透的今天,企业管理已从“经验驱动”全面转向“数据驱动”,数据思维成为企业高质量发展的核心竞争力,而CDA ...
2026-03-12在数字经济飞速发展的今天,数据分析已从“辅助工具”升级为“核心竞争力”,渗透到商业、科技、民生、金融等各个领域。无论是全 ...
2026-03-11上市公司财务报表是反映企业经营状况、盈利能力、偿债能力的核心数据载体,是投资者决策、研究者分析、从业者复盘的重要依据。16 ...
2026-03-11数字化浪潮下,数据已成为企业生存发展的核心资产,而数据思维,正是CDA(Certified Data Analyst)数据分析师解锁数据价值、赋 ...
2026-03-11线性回归是数据分析中最常用的预测与关联分析方法,广泛应用于销售额预测、风险评估、趋势分析等场景(如前文销售额预测中的多元 ...
2026-03-10在SQL Server安装与配置的实操中,“服务名无效”是最令初学者头疼的高频问题之一。无论是在命令行执行net start启动服务、通过S ...
2026-03-10在数据驱动业务的当下,CDA(Certified Data Analyst)数据分析师的核心价值,不仅在于解读数据,更在于搭建一套科学、可落地的 ...
2026-03-10在企业经营决策中,销售额预测是核心环节之一——无论是库存备货、营销预算制定、产能规划,还是战略布局,都需要基于精准的销售 ...
2026-03-09金融数据分析的核心价值,是通过挖掘数据规律、识别风险、捕捉机会,为投资决策、风险控制、业务优化提供精准支撑——而这一切的 ...
2026-03-09在数据驱动决策的时代,CDA(Certified Data Analyst)数据分析师的核心工作,是通过数据解读业务、支撑决策,而指标与指标体系 ...
2026-03-09在数据处理的全流程中,数据呈现与数据分析是两个紧密关联却截然不同的核心环节。无论是科研数据整理、企业业务复盘,还是日常数 ...
2026-03-06在数据分析、数据预处理场景中,dat文件是一种常见的二进制或文本格式数据文件,广泛应用于科研数据、工程数据、传感器数据等领 ...
2026-03-06在数据驱动决策的时代,CDA(Certified Data Analyst)数据分析师的核心价值,早已超越单纯的数据清洗与统计分析,而是通过数据 ...
2026-03-06在教学管理、培训数据统计、课程体系搭建等场景中,经常需要对课时数据进行排序并实现累加计算——比如,按课程章节排序,累加各 ...
2026-03-05在数据分析场景中,环比是衡量数据短期波动的核心指标——它通过对比“当前周期与上一个相邻周期”的数据,直观反映指标的月度、 ...
2026-03-05