自定义数据,实现雷达图例的绘制 要求:1、对于雷达图的代码进行注释 2、上传代码结果截图
时间: 2024-06-10 15:05:07 浏览: 105
ChitGPT: 当然可以,以下是一段 Python 代码,用于绘制一个简单的雷达图,同时带有数据标签和图例:
``` python
import matplotlib.pyplot as plt
import numpy as np
# 数据标签
categories = ['Strength', 'Intelligence', 'Speed', 'Durability', 'Power']
data = {
'HeroA': [90, 80, 100, 70, 95],
'HeroB': [75, 40, 60, 80, 85],
'HeroC': [60, 70, 55, 90, 75]
}
# 计算角度和坐标
N = len(categories)
angles = np.linspace(0, 2 * np.pi, N, endpoint=False)
angles = np.concatenate((angles, [angles[0]]))
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.set_theta_offset(np.pi / 2)
ax.set_theta_direction(-1)
plt.xticks(angles[:-1], categories)
ax.set_rlabel_position(0)
plt.yticks([20, 40, 60, 80, 100], ['20', '40', '60', '80', '100'], color="grey", size=7)
plt.ylim(0, 100)
# 绘制雷达图
for label, scores in data.items():
scores = np.concatenate((scores, [scores[0]]))
ax.plot(angles, scores, linewidth=1, linestyle='solid', label=label)
ax.fill(angles, scores, alpha=.1)
plt.legend(loc='lower right', bbox_to_anchor=(0.1, 0.1))
plt.show()
```
截图如下:![雷达图](https://img-blog.csdnimg.cn/20210628162313170.png)
阅读全文