import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams['font.family'] = 'SimHei' matplotlib.rcParams['font.sans-serif'] = ['SimHei'] radar_labels = np.array(['爆发力', '技巧熟练度', '威慑力', '稳定性', '侵入性', '防御性']) nAttr = len(radar_labels) data = np.array([ [0.8, 0.9, 0.75, 0.9, 0.9, 0.8], # 注意最后添加了马龙的数据,为了闭合雷达图 [0.85, 0.87, 0.85, 0.9, 0.8, 0.85], [0.9, 0.6, 0.8, 0.6, 0.89, 0.9], [0.8, 0.7, 0.7, 0.9, 0.8, 0.8], [0.6, 0.9, 0.9, 0.921, 0.87, 0.6] ]) data_labels = ('马龙', '张继科', '马琳', '王励勤', '樊振东') angles = np.linspace(0, 2 * np.pi, nAttr, endpoint=False).tolist() angles += angles[:1] fig, ax = plt.subplots(subplot_kw=dict(polar=True)) # 分别绘制每位运动员的数据 for i, (d, label) in enumerate(zip(data, data_labels), 1): ax.plot(angles, np.concatenate((d, [d[0]])), color=f'C{i - 1}', linewidth=2, label=label) ax.fill(angles, np.concatenate((d, [d[0]])), color=f'C{i - 1}', alpha=0.25) angle_ticks = np.linspace(0, 2 * np.pi, nAttr, endpoint=False) ax.set_xticks(angle_ticks) ax.set_xticklabels(radar_labels) ax.set_title('中国国家队乒乓球运动员参数雷达图', ha='right', size=15) legend = plt.legend(loc=(0.9, 0.92), labelspacing=0.1) plt.grid(True) plt.savefig('dota_radar.JPG') # 修正了空格 plt.show()
代码运行后的图: