python制作雷达图_python批量制作雷达图的实现方法——CDA人工智能学院

CDA人工智能学院致力于以优质的数据分析、我即可获取CDA会员1个月免费试听机会

因为工作需要有时候要画雷达图,但是数据好多组怎么办?不能一个一个点excel去画吧,那么可以利用

2020-11-18 13:26:26 上传

下载附件 (24.83 KB)

首先制作一个演示的excel,评分为excel随机数生成:

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

加入标签等得到的excel样式如下(部分,共计32行):

2020-11-18 13:26:41 上传

下载附件 (37.32 KB)

那么接下来就是打开

2020-11-18 13:26:58 上传

下载附件 (16.47 KB)

将第一列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)

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), va='bottom', fontproperties="SimHei")

ax.set_rlim(3.8,5)# 设置雷达图的范围

ax.grid(True)

plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品货号:" + str(info_id) + ".png", dpi=120)

图片太大怎么办?用库改变大小即可:

import Image

##更改图片大小

infile = “C:\\Users\\Administrator\\Desktop\\result\\商品货号:" + str(info_id) + ".png“

outfile = ”C:\\Users\\Administrator\\Desktop\\result1\\商品货号:" + str(info_id) + ".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) + '.png') ##写入图片

time.sleep(1)##防止写入太快电脑死机

plt.close() #  一定要关掉图片,不然

得到的效果如下:

2020-11-18 13:27:35 上传

下载附件 (85.28 KB)

附上完整代码:

import )

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), va='bottom', fontproperties="SimHei")

ax.set_rlim(3.8, 5) # 设置雷达图的范围

ax.grid(True)

plt.savefig("C:\\Users\\Administrator\\Desktop\\result\\商品货号:" + str(info_id) + ".png", dpi=120)

# plt.show()在) + ".png"

outfile = "C:\\Users\\Administrator\\Desktop\\result1\\商品货号:" + str(info_id) + ".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) + '.png') ##写入图片

time.sleep(1) ##防止写入太快电脑死机

plt.close() # 一定要关掉图片,不然

2020-11-18 13:27:57 上传

下载附件 (10.55 KB)

关注“CDA人工智能学院”,回复“录播”获取更多人工智能精选直播视频!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值