python画图代码动态爱心
时间: 2023-11-11 08:00:46 浏览: 46
好的,以下是Python画动态爱心的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
def draw_heart():
t = np.linspace(0, 2*np.pi, 1000)
x = 16 * np.sin(t)**3
y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
plt.plot(x, y, color='red', linewidth=2)
plt.axis('equal')
plt.axis('off')
plt.show()
def animate_heart():
fig, ax = plt.subplots()
t = np.linspace(0, 2*np.pi, 1000)
x = 16 * np.sin(t)**3
y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
line, = ax.plot(x, y, color='red', linewidth=2)
ax.axis('equal')
ax.axis('off')
def update(num):
line.set_data(x[:num], y[:num])
return line,
ani = plt.animation.FuncAnimation(fig, update, frames=range(1000), interval=20)
plt.show()
# draw_heart() # 静态爱心
animate_heart() # 动态爱心
```