【Python3.8】使用 matplotlib 绘制雷达图

目录

一、需求

二、代码实现

三、代码分析

1.angles = np.linspace(0,2*np.pi,data_length,endpoint=False)

2.score_a = np.concatenate((score[0], [score[0][0]]))

3.score_a = [int(i) for i in score[0]]

4.ax.plot(angles, score_a, color="b")

5.ax.set_thetagrids(angles*180/np.pi, labels)

6.plt.savefig('temp2.png')


一、需求

【Python3.8】使用 matplotlib 绘制雷达图_第1张图片

scoreRadar.txt:

专业	C语言	Java	Python	C#	Javascript
软件工程 	95	96	85	63	91
计算机科学与技术	75	93	66	85	88
网络工程 	86	76	96	93	67

二、代码实现

直接上代码,代码段后再详细分析:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
# 使能够读取中文
matplotlib.rcParams['font.family'] = 'SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
filename = './scoreRadar.txt'
fp = open(filename, 'r', encoding='UTF-8')
List = []
flag = 0
s = fp.readline()
while s:
    # 去除换行符
    s = s.strip('\n')
    new_s = s.split("\t")
    List.append(new_s)
    s = fp.readline()
print(List)
data_length = len(List[0]) - 1
angles = np.linspace(0,2*np.pi,data_length,endpoint=False)
labels = List[0][1:]
score = [item[1:] for item in List[1:]]
print(labels,score)
# 将数据首尾连接,使得画出的线闭合
score_a = np.concatenate((score[0], [score[0][0]]))
score_b = np.concatenate((score[1], [score[1][0]]))
score_c = np.concatenate((score[2], [score[2][0]]))
angles = np.concatenate((angles, [angles[0]]))
labels = np.concatenate((labels, [labels[0]]))

score_a = [int(i) for i in score_a]
score_b = [int(i) for i in score_b]
score_c = [int(i) for i in score_c]
fig = plt.figure(dpi=100)
# 新建一个子图
ax = plt.subplot(111, projection='polar')
# 绘制雷达图
ax.plot(angles, score_a, color="b")
ax.plot(angles, score_b, color="g")
ax.plot(angles, score_c, color="r")
ax.set_rgrids(np.arange(20,120,20))
# 设置雷达图中每一项的标签显示
ax.set_thetagrids(angles*180/np.pi, labels)
# 设置雷达图的0度起始位置
ax.set_theta_zero_location("E")
# 设置雷达图的坐标值显示角度,相对于起始角度的偏移量
ax.set_rlabel_position(30)
ax.set_title("成绩雷达图")
plt.savefig('temp2.png')
plt.show()

三、代码分析

想要使用 matplotlib 绘制出雷达图,就需要使用其中的极坐标绘图。

在此分析及记录一些重要的代码和易错点。

1.angles = np.linspace(0,2*np.pi,data_length,endpoint=False)

作为序列生成器, numpy.linspace()函数用于在线性空间中以均匀步长生成数字序列。

这里使用序列生成器,均匀生成雷达图中每个点相对于极轴的角度(弧度制)。

2.score_a = np.concatenate((score[0], [score[0][0]]))

numpy.concatenate() 函数用于完成数组的拼接。

这里是指使雷达图的点数据首尾相连,形成封闭图形,如果不作封闭,最后绘制结果会如下图所示:

【Python3.8】使用 matplotlib 绘制雷达图_第2张图片

3.score_a = [int(i) for i in score[0]]

这里使用列表生成器将列表内的所有数据转成int类型。不转成int类型将无法正确绘制雷达图。

当然也可以使用如下语句完成类型转换:

score_a = list(map(int,score[0]))

4.ax.plot(angles, score_a, color="b")

这句语句用于绘制雷达图中的数据线段。

需要注意的是angles和score_必须拥有相同的元素数量(这是很容易踩到的一个坑),否则无法绘制!

5.ax.set_thetagrids(angles*180/np.pi, labels)

用于绘制标签。

同上条原理,angles和labels必须拥有相同的元素数量。

6.plt.savefig('temp2.png')

注意,保存图片的代码必须在.show()方法之前,否则会保存出空白的图片,这一点非常重要。

你可能感兴趣的:(Python,python,matplotlib)