用python在雷达图内画一个红色五角星
时间: 2024-10-15 19:12:02 浏览: 29
在Python中,我们可以使用matplotlib库来创建雷达图,并且可以利用一些额外的库如`patch`模块来绘制五角星形状。以下是一个简单的例子:
```python
import matplotlib.pyplot as plt
from matplotlib.path import Path
import numpy as np
# 创建雷达图的基本数据点
labels = ['A', 'B', 'C', 'D', 'E']
values = [50, 75, 40, 80, 60]
# 创建一个等边五角星的Path对象
vertices = [
(0.8660254037844386, 0.5), # 第一个顶点
(0.7071067811865476, -0.7071067811865476),
(-0.7071067811865476, -0.7071067811865476),
(-0.8660254037844386, 0.5),
(-0.8660254037844386, -0.5)
]
codes = [Path.MOVETO,
Path.LINETO] * 5 + [Path.CLOSEPOLY]
path = Path(vertices, codes)
fig, ax = plt.subplots()
ax.set_yticklabels([])
ax.set_xticklabels(labels, rotation=45, ha="right")
# 将五角星路径转换为Polygon并填充颜色
star = path patch = patches.PathPatch(path, facecolor='red')
ax.add_patch(star)
for value in values:
angles = np.deg2rad(range(0, 360, 360 / len(values)))
ax.plot(np.cos(angles) * value, np.sin(angles) * value, color='black')
plt.title(' Radar Chart with a Red Five-pointed Star ')
plt.show()
```
阅读全文