#新增加的两行
import matplotlib
matplotlib.rc("font",family='SimHei')
import numpy as np
import matplotlib.pyplot as plt
# 定义能力维度
labels=np.array(['团队意识', '质量意识', '自动化意识', '创新意识', '工作热情', '改进意识'])
# 原始数据和变化后的对比数据
values_before = np.array([7, 6, 5, 6, 7, 6])
values_after = np.array([9, 8, 9, 9, 8, 8])
# 计算角度
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False).tolist()
# 使雷达图封闭
values_before=np.concatenate((values_before,[values_before[0]]))
values_after=np.concatenate((values_after,[values_after[0]]))
angles+=angles[:1]
# 画图
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(polar=True))
# 雷达填充
ax.fill(angles, values_before, color='salmon', alpha=0.5)
ax.fill(angles, values_after, color='lightblue', alpha=0.5)
# 添加连线
ax.plot(angles, values_before, color='red', linewidth=1.5, linestyle='dashed')
ax.plot(angles, values_after, color='blue', linewidth=1.5, linestyle='dashed')
# 标注数值
for i in range(len(labels)):
ax.annotate(values_before[i], (angles[i], values_before[i]), textcoords="offset points", xytext=(0,10), ha='center', fontsize=12, color='darkred')
ax.annotate(values_after[i], (angles[i], values_after[i]), textcoords="offset points", xytext=(0,10), ha='center', fontsize=12, color='darkblue')
# 设置标签
ax.set_yticklabels([])
ax.set_xticks(angles[:-1])
ax.set_xticklabels(labels, fontsize=12)
# 添加标题
plt.title('能力雷达图', size=20, color='black', y=1.1)
# 添加图例
plt.legend(['QC前', 'QC后'], loc='upper right')
plt.show()
python--绘制雷达图
于 2024-03-25 17:24:13 首次发布