python画图心形_python如何绘制心形

python绘制心形的方法:利用matplotlib和numpy画心形,代码为【init = np.arange(-np.pi, np.pi, 0.001);plt.fill_between(x, y, facecolor='red')】。

2020092711254378446.jpg

python绘制心形的方法:

python 利用 matplotlib 和 numpy 画心形。import matplotlib.pyplot as plt

import numpy as np

#初始化数据

init = np.arange(-np.pi, np.pi, 0.001)

y = np.subtract(np.multiply(2, np.cos(init)), np.cos(np.multiply(2, init)))

x = np.subtract(np.multiply(2, np.sin(init)), np.sin(np.multiply(2, init)))

#画图

plt.plot(x, y)

plt.fill_between(x, y, facecolor='red')

plt.show()

结果:

1601177113641209.png

以上就是python如何绘制心形的详细内容,更多请关注php中文网其它相关文章!

article_wechat2021.jpg?1111

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

以下是使用Python的turtle模块画出动态心形的示例代码: ```python import turtle import math # 设置画布背景色和画笔颜色 turtle.bgcolor("black") turtle.pencolor("pink") # 设置画笔粗细和速度 turtle.pensize(2) turtle.speed(10) # 定义心形函数 def heart(t, x, y, size): t.penup() t.goto(x, y) t.pendown() t.setheading(0) t.begin_fill() t.fillcolor("red") t.pensize(size) for i in range(200): t.right(1) a = math.sin(math.radians(i)) x = size * a * math.cos(math.radians(i)) y = size * a * math.sin(math.radians(i)) t.goto(x, y) t.end_fill() # 定义动态心形函数 def dynamic_heart(): for i in range(200, 600, 5): turtle.clear() heart(turtle, 0, -i/10, i/30) turtle.right(5) # 调用动态心形函数 dynamic_heart() # 隐藏画笔 turtle.hideturtle() # 点击关闭窗口 turtle.exitonclick() ``` 代码解释: 首先,我们通过导入turtle模块来实现画图功能。然后,我们设置画布的背景色为黑色,画笔颜色为粉色,并设置画笔的粗细和速度。接下来,我们定义了一个heart函数来绘制心形,该函数具有四个参数:t,x,y和size。其,t表示turtle对象,x和y表示心形心坐标,size表示心形的大小。 在heart函数,我们使用turtle的goto方法和sin、cos函数来绘制心形。具体来说,我们使用for循环来旋转turtle对象,然后计算出每个角度对应的x和y坐标,并使用goto方法来移动turtle对象。 接下来,我们定义了一个dynamic_heart函数来实现动态心形。该函数使用for循环来旋转turtle对象,并在每个角度上调用heart函数来绘制心形。我们还使用了turtle的clear方法来清除之前绘制的图形。 最后,我们隐藏了画笔,并通过调用exitonclick方法来等待用户点击关闭窗口。 运行代码后,我们可以看到一个动态的心形图案在窗口不断旋转。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值