使用matplotlib绘小猪佩奇
时间: 2023-11-22 20:48:44 浏览: 90
很高兴回答您的问题!
以下是使用matplotlib绘制小猪佩奇的步骤:
1.导入需要的库和模块
```python
import matplotlib.pyplot as plt
import numpy as np
```
2.创建画布和子图
```python
fig, ax = plt.subplots()
```
3.绘制小猪佩奇的头部
```python
# 绘制头部
circle1 = plt.Circle((0, 0), 0.5, color='pink')
ax.add_artist(circle1)
# 绘制鼻子
circle2 = plt.Circle((0.2, 0.1), 0.1, color='black')
ax.add_artist(circle2)
# 绘制眼睛
circle3 = plt.Circle((-0.15, 0.2), 0.1, color='black')
ax.add_artist(circle3)
circle4 = plt.Circle((0.15, 0.2), 0.1, color='black')
ax.add_artist(circle4)
# 绘制腮红
circle5 = plt.Circle((-0.35, -0.15), 0.15, color='pink')
ax.add_artist(circle5)
circle6 = plt.Circle((0.35, -0.15), 0.15, color='pink')
ax.add_artist(circle6)
```
4.绘制小猪佩奇的身体
```python
# 绘制身体
rect1 = plt.Rectangle((-0.5, -0.5), 1, 1, color='pink')
ax.add_artist(rect1)
# 绘制尾巴
tail = np.array([[-0.5, -0.5], [-0.7, -0.3], [-0.7, 0.3], [-0.5, 0.5]])
tail_line = plt.Polygon(tail, color='pink')
ax.add_artist(tail_line)
# 绘制四条腿
leg1 = plt.Rectangle((-0.4, -0.5), 0.2, 0.5, color='black')
ax.add_artist(leg1)
leg2 = plt.Rectangle((0.2, -0.5), 0.2, 0.5, color='black')
ax.add_artist(leg2)
leg3 = plt.Rectangle((-0.5, -0.5), 0.2, 0.5, color='black')
ax.add_artist(leg3)
leg4 = plt.Rectangle((0.3, -0.5), 0.2, 0.5, color='black')
ax.add_artist(leg4)
```
5.设置坐标轴范围和隐藏坐标轴
```python
# 设置坐标轴范围
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
# 隐藏坐标轴
plt.axis('off')
```
6.显示绘制结果
```python
plt.show()
```
阅读全文