12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
编程知识 时间:2024-11-29 10:17:38
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
如果你对Python的绘图库matplotlib有所熟悉,那么你能用Python画出一只可爱的猪。我们来看一下如何实现。首先,利用matplotlib的pyplot模块来创建一个绘画板,绘制出一个圆圈作为
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
如果你对Python的绘图库matplotlib有所熟悉,那么你能用Python画出一只可爱的猪。我们来看一下如何实现。
首先,利用matplotlib的pyplot模块来创建一个绘画板,绘制出一个圆圈作为猪的头部:
import matplotlib.pyplot as plt# 创建画布,大小为6x6fig = plt.figure(figsize=[6,6])# 画出一个圆作为猪的头部circle = plt.Circle((3,3),2, color='pink')fig.gca().add_artist(circle)
接下来,利用matplotlib的patches模块来绘制猪的眼睛、脸颊和鼻子:
from matplotlib.patches import Arc# 绘制两只眼睛,圆弧形状left_eye = Arc((2.3,3), 0.5, 1, angle=0, theta1=0, theta2=180, color='black')right_eye = Arc((3.7,3), 0.5, 1, angle=0, theta1=0, theta2=180, color='black')fig.gca().add_artist(left_eye)fig.gca().add_artist(right_eye)# 绘制两个圆形的脸颊left_cheek = plt.Circle((1.5,2.5),0.5, color='pink')right_cheek = plt.Circle((4.5,2.5),0.5, color='pink')fig.gca().add_artist(left_cheek)fig.gca().add_artist(right_cheek)# 绘制鼻子和口nose = plt.Circle((3,2.2),0.25, color='black')mouth = Arc((3,2.2), 1.2, 0.6, angle=0, theta1=20, theta2=160, color='black', lw=1.5)fig.gca().add_artist(nose)fig.gca().add_artist(mouth)
最后,我们可以将画布保存为一个图片文件,以便日后使用:
# 关闭坐标轴显示plt.axis('off')# 保存为图片plt.savefig('pig.png', dpi=150, bbox_inches='tight')
这就是使用Python利用matplotlib库绘制出一只可爱的猪的全部代码。你可以根据自己的喜好调整参数,画出有趣的各种形态的猪。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19