matplotlib绘制红色五角星
时间: 2024-03-16 19:38:26 浏览: 150
要使用matplotlib库绘制红色五角星,可以按照以下步骤进行操作:
1. 导入matplotlib库和numpy库:
```python
import matplotlib.pyplot as plt
import numpy as np
```
2. 创建一个新的图形对象和一个子图对象:
```python
fig, ax = plt.subplots()
```
3. 定义五角星的坐标点:
```python
points = np.array([[0, 0.5], [0.09, 0.15], [0.4, 0.15], [0.15, -0.05], [0.25, -0.4], [0, -0.15], [-0.25, -0.4], [-0.15, -0.05], [-0.4, 0.15], [-0.09, 0.15]])
```
4. 绘制五角星并设置颜色为红色:
```python
star = plt.Polygon(points, closed=True, fill=None, edgecolor='red')
ax.add_patch(star)
```
5. 设置坐标轴范围:
```python
ax.set_xlim(-0.5, 0.5)
ax.set_ylim(-0.5, 0.5)
```
6. 隐藏坐标轴刻度和标签:
```python
ax.set_xticks([])
ax.set_yticks([])
```
7. 显示图形:
```python
plt.show()
```
完整的代码如下所示:
```python
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
points = np.array([[0, 0.5], [0.09, 0.15], [0.4, 0.15], [0.15, -0.05], [0.25, -0.4], [0, -0.15], [-0.25, -0.4], [-0.15, -0.05], [-0.4, 0.15], [-0.09, 0.15]])
star = plt.Polygon(points, closed=True, fill=None, edgecolor='red')
ax.add_patch(star)
ax.set_xlim(-0.5, 0.5)
ax.set_ylim(-0.5, 0.5)
ax.set_xticks([])
ax.set_yticks([])
plt.show()
```
希望这个代码能够帮助到你!如果有任何问题,请随时提问。
阅读全文