Python跳动爱心
时间: 2025-01-15 19:09:53 浏览: 51
### Python 实现动态心形图案效果
为了创建一个动态变化的心形图案,可以利用 `matplotlib` 和 `numpy` 库来完成这一目标。下面是一个具体的例子:
```python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(t):
theta = np.linspace(0, 2 * np.pi, 1000)
x = 16 * np.sin(theta)**3
y = -(13 * np.cos(theta) - 5 * np.cos(2 * theta) -
2 * np.cos(3 * theta) - np.cos(4 * theta))
# Add time-based transformation to create dynamic effect
scale_factor = (np.sin(t / 10) + 1) * 0.5 + 0.5
x *= scale_factor
y *= scale_factor
line.set_data(x, y)
return line,
ani = FuncAnimation(fig, animate, frames=np.arange(0, 200),
init_func=init, blit=True)
plt.show()
```
这段代码通过调整心形参数方程中的比例因子随时间的变化而变化,实现了心形图标的缩放动画效果[^1]。
此外,还可以使用 `Turtle` 库来制作更加直观的动态心形图案。这种方式更适合初学者理解绘图过程,并且能够更灵活地控制每一帧的画面更新[^3]。
阅读全文
相关推荐

















