99网
您的当前位置:首页matplotlib制作雷达图

matplotlib制作雷达图

来源:99网

matplotlib制作雷达图

import numpy as np
import matplotlib.pyplot as plt
# 用于正常显示中文
plt.rcParams['font.sans-serif'] = 'SimHei'
# 用于正常显示符号
plt.rcParams['axes.unicode_minus'] = False
 
# 使用ggplot的绘图风格
plt.style.use('ggplot')
 
# 构造数据
values = [0.6,0.8,0.4,0.7,0.9,1.0]
values_2=[0.7,0.5,0.9,0.8,0.8,1.0]
feature = ['个人能力','QC知识','解决问题能力','服务质量意识','团队精神','测试']

# 设置每个数据点的显示位置,在雷达图上用角度表示
# np.linspace:在指定的间隔内返回均匀间隔的数字。
angles=np.linspace(0, 2*np.pi,len(values), endpoint=False)
 
# 拼接数据首尾,使图形中线条封闭
# np.concatenate:数组拼接
values=np.concatenate((values,[values[0]]))
values_2=np.concatenate((values_2,[values_2[0]]))
angles=np.concatenate((angles,[angles[0]]))
feature=np.concatenate((feature,[feature[0]]))

# 绘图
fig=plt.figure()
# 设置为极坐标格式,111:一行一列第一个图(221:二行两列第一个图)
ax = fig.add_subplot(111, polar=True)
# 绘制折线图,o-:点状实线图
ax.plot(angles, values, 'o-', linewidth=2,label='活动前')
ax.plot(angles, values_2, 'o-', linewidth=2,label='活动后')
# 填充颜色
ax.fill(angles, values, alpha=0.25)
ax.fill(angles, values_2, alpha=0.25)
 
# 设置图标上的角度划分刻度,为每个数据点处添加标签
ax.set_thetagrids(angles * 180/np.pi, feature)
 
# 设置雷达图的范围
ax.set_ylim(0,1)
# 添加标题
plt.title('活动前后员工状态表现')
# 添加网格线
ax.grid(True)
plt.legend() #显示图例
plt.show()

结果如下:

参考以下博客
[1]: https:///zx1245773445/article/details/96992357

因篇幅问题不能全部显示,请点此查看更多更全内容