python绘制笑脸
时间: 2024-09-28 13:16:17 浏览: 45
在Python中,你可以使用matplotlib库来绘制简单的笑脸图形。下面是一个基本的例子,展示如何使用圆和弧线组合来创建一个笑脸:
```python
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Arc
# 创建一个新的图形
fig, ax = plt.subplots()
# 创建眼睛、鼻子和嘴巴的元素
eye_size = 0.1
eye_center = (0.5, 0.4)
nose_length = 0.15
mouth_width = 0.3
mouth_height = 0.1
mouth_top = 0.6
# 眼睛
circle_eyebrow = Circle(eye_center, eye_size / 2, facecolor='black', edgecolor='white')
circle_eye = Circle(eye_center, eye_size, facecolor='white', edgecolor='black')
# 鼻子
arc_nose = Arc((eye_center[0], eye_center[1] + nose_length), mouth_width, mouth_height,
theta1=180, theta2=270, color='red')
# 嘴巴
angle_mouth = 90 - mouth_height / 2
mouth_left = (eye_center[0] - mouth_width / 2 * np.cos(np.radians(angle_mouth)),
eye_center[1] + mouth_width / 2 * np.sin(np.radians(angle_mouth)))
mouth_right = (eye_center[0] + mouth_width / 2 * np.cos(np.radians(angle_mouth)),
eye_center[1] + mouth_width / 2 * np.sin(np.radians(angle_mouth)))
line_mouth = plt.Line2D([mouth_left[0], mouth_right[0]], [mouth_left[1], mouth_right[1]],
linewidth=2, color='black')
# 添加到图形上
ax.add_patch(circle_eyebrow)
ax.add_patch(circle_eye)
ax.add_patch(arc_nose)
ax.add_line(line_mouth)
# 设置坐标轴可见性
ax.axis('off')
# 显示图形
plt.show()
```
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)