python用matplotlib画五星红旗
时间: 2023-10-10 21:14:48 浏览: 309
python应用实例
以下是用Python的Matplotlib库绘制五星红旗的代码:
```python
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, ax = plt.subplots()
# 绘制红色背景
rect = patches.Rectangle((0, 0), width=30, height=20, facecolor='red')
ax.add_patch(rect)
# 绘制大星星
star1 = patches.Polygon([(5, 15), (7.5, 13), (10, 15), (8.5, 10), (9.5, 7), (5, 9), (0.5, 7), (1.5, 10), (-0.5, 15)], facecolor='yellow')
ax.add_patch(star1)
# 绘制四个小星星
star2 = patches.Polygon([(13, 18), (14, 17.5), (15, 18), (14.5, 17)], facecolor='yellow')
ax.add_patch(star2)
star3 = patches.Polygon([(14, 15), (14.5, 15.5), (15, 15), (14.5, 14.5)], facecolor='yellow')
ax.add_patch(star3)
star4 = patches.Polygon([(14, 12), (14.5, 11.5), (15, 12), (14.5, 12.5)], facecolor='yellow')
ax.add_patch(star4)
star5 = patches.Polygon([(13, 9), (14, 9.5), (15, 9), (14.5, 8)], facecolor='yellow')
ax.add_patch(star5)
plt.axis('equal')
plt.axis('off')
plt.show()
```
运行代码后,将得到以下五星红旗的图形:
![五星红旗](https://i.loli.net/2021/09/23/5QNz1xwJHkM8AVS.png)
阅读全文