利用Python中numpy库和matplotlib库编程一个有12个点,8个区域的雷达图
时间: 2024-02-18 13:05:34 浏览: 22
好的,以下是实现代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 数据
data = np.array([[60, 60, 60, 60, 60, 60, 60, 60],
[80, 70, 50, 30, 20, 30, 50, 70],
[90, 80, 60, 40, 20, 40, 60, 80],
[95, 85, 70, 50, 30, 50, 70, 85],
[95, 90, 80, 60, 40, 60, 80, 90],
[95, 95, 90, 70, 50, 70, 90, 95],
[95, 95, 95, 80, 60, 80, 95, 95],
[95, 95, 95, 90, 80, 90, 95, 95],
[100, 100, 100, 95, 90, 95, 100, 100],
[100, 100, 100, 100, 95, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100]])
# 极坐标系角度
angles = np.linspace(0, 2 * np.pi, data.shape[1], endpoint=False)
# 将第一个数据点重复到最后一个点,以闭合雷达图
data = np.concatenate((data, data[:, :1]), axis=1)
# 将第一个角度重复到最后一个角度,以闭合雷达图
angles = np.concatenate((angles, [angles[0]]))
# 绘图
fig = plt.figure()
# 设置极坐标系
ax = fig.add_subplot(111, polar=True)
# 绘制每个区域的数据
for i in range(data.shape[0]):
ax.plot(angles, data[i], linewidth=1)
# 添加标签
ax.set_thetagrids(angles[:-1] * 180 / np.pi, labels=None)
ax.set_rgrids(range(20, 121, 20), angle=0, labels=None)
ax.set_title('Radar Chart')
ax.legend()
# 显示图形
plt.show()
```
这个程序会生成一个12个点,8个区域的雷达图,如下所示:
![image.png](attachment:image.png)