介绍一、?月亮
这篇文章主要介绍”利用python画出月亮和月饼代码实例”,在日常操作中,相信很多人在利用python画出月亮和月饼代码实例问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答“利用python画出月亮和月饼代码实例”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
<编辑类="目录">目录编辑>- <李>
一、?月亮
二、?雪花月饼
李>
一、?月亮
<>强导入库<代码> matplotlib 代码>和<代码> numpy> 代码,作为工具直接用。强>
得到mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot  as plt import numpy as  np
<强>布局强>
创建一个<代码>长×宽> 代码的一个画布大小
fig =, plt.figure (figsize=(10、5))
<强>布置3 d视角强>
ax =, fig.add_subplot(121年,投影=& # 39;3 d # 39;)
<强>生成等差数列的数据,画出一个<代码>球体代码> 强>
x =, np.linspace (0, 2, *, np.pi,, 100) 时间=y np.linspace (0,, np.pi,, 100) 时间=x1 10, *, np.outer (np.cos (x), np.sin (y)) 时间=y1 10, *, np.outer (np.sin (x), np.sin (y)) 时间=z 10, *, np.outer (np.ones (np.size (x1)),, np.cos (y1)) ax.plot_surface (x1, y1,, z,,颜色=& # 39;黄色# 39;)
<强>此时我们发现只画了一个月亮,所以我们打算画第二个月亮,凑成一对~ 强>
ax =, fig.add_subplot(122年,投影=& # 39;3 d # 39;) 时间=x np.linspace (0, 2, *, np.pi,, 100) 时间=y np.linspace (0,, np.pi,, 100) 时间=x1 10, *, np.outer (np.cos (x), np.sin (y)) 时间=y1 10, *, np.outer (np.sin (x), np.sin (y)) 时间=z 10, *, np.outer (np.ones (np.size (x1)),, np.cos (y1)) ax.plot_surface (x1, y1,, z,,颜色=& # 39;" # 39;)
<>强效果图强>
还可以吧~哈哈哈哈!
二、?雪花月饼
<>强导入所需要的库,用的比较多的是乌龟<代码> 代码>这个库来写的~ 强>
import 海龟 import 时间
<强>我们先画一个底层的黄黄的饼作为底部~ 强>
def 圆(): ,,,fillX_y (0, 0) ,,,turtle.color (“# F0E68C") ,,,for _ 拷贝范围(30): ,,,,,,,turtle.left (12) ,,,,,,,turtle.begin_fill () ,,,,,,,turtle.forward (230) ,,,,,,,turtle.circle (25, 180) ,,,,,,,turtle.goto (0, 0) ,,,,,,,turtle.left (180) ,,,,,,,turtle.end_fill () ,,,,,,,time . sleep (0.5)
既然是<代码>雪花月饼> 代码,就应该是有雪的花,接下来我们来画<代码>雪花> 代码吧!
采用<代码>科赫曲线代码>进行画雪花。要改变弯曲程度。
def 科赫(大小,,n):,, #,定义弯曲的直线使其有棱角 ,,,if n ==, 0: ,,,,,,,turtle.fd(大小) ,,,: ,,,,,,,for angle 拷贝[0,,60岁,-120年,60岁):,,#,每一次都对其进行合理的角度改变 ,,,,,,,,,,,turtle.left(角) ,,,,,,,,,,,科赫(时间/size 3, n 作用;1)
<强>因为打算画八个,并且加上<代码>颜色> 代码,所以<代码>七彩雪花代码> ~ 强>
if a ==, 1: ,,,turtle.color (“black"),, #,控制画笔的颜色 elif a ==, 2: ,,,turtle.color (“purple") elif a ==, 3: ,,,turtle.color (“blue") elif a ==, 4: ,,,turtle.color (“Cyan") elif a ==, 5: ,,,turtle.color (“green") elif a ==, 6: ,,,turtle.color (“yellow") elif a ==, 7: ,,,turtle.color (“orange") elif a ==, 8: ,,,turtle.color (“red")利用python画出月亮和月饼代码实例