results = [{"力量": 7, "防御": 10, "速度": 7, "智力": 2, "暴击": 5,"格挡": 8}, {"力量": 2, "防御": 5, "速度": 10, "智力": 8, "暴击": 9,"格挡": 4}] python生成雷达图代码
时间: 2023-12-21 16:31:18 浏览: 79
以下是使用Python生成雷达图的代码示例:
```python
import matplotlib.pyplot as plt
import numpy as np
# 数据
results = [{"力量": 7, "防御": 10, "速度": 7, "智力": 2, "暴击": 5, "格挡": 8},
{"力量": 2, "防御": 5, "速度": 10, "智力": 8, "暴击": 9, "格挡": 4}]
# 提取属性和属性值
attributes = list(results[0].keys())
values = np.array([list(result.values()) for result in results])
# 计算角度
angles = np.linspace(0, 2 * np.pi, len(attributes), endpoint=False).tolist()
angles += angles[:1]
# 绘制雷达图
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'polar': True})
ax.fill(angles, values[0].tolist() + values[0][:1].tolist(), alpha=0.25)
ax.plot(angles, values[0].tolist() + values[0][:1].tolist(), linewidth=1, linestyle='solid', label='Player 1')
ax.fill(angles, values[1].tolist() + values[1][:1].tolist(), alpha=0.25)
ax.plot(angles, values[1].tolist() + values[1][:1].tolist(), linewidth=1, linestyle='solid', label='Player 2')
# 设置属性标签
ax.set_xticks(angles[:-1])
ax.set_xticklabels(attributes)
# 设置图例
ax.legend(loc='upper right', bbox_to_anchor=(1.3, 1))
# 显示图形
plt.show()
```
阅读全文