import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots(figsize=(6, 6))

def heart(t, scale=1):
    x = 16 * np.sin(t) ** 3 * scale
    y = (13 * np.cos(t) - 5 * np.cos(2 * t)
         - 2 * np.cos(3 * t) - np.cos(4 * t)) * scale
    return x, y

t = np.linspace(0, 2 * np.pi, 500)
line, = ax.plot([], [], color='red', linewidth=3)
fill = None

def init():
    ax.set_xlim(-20, 20)
    ax.set_ylim(-20, 20)
    ax.set_aspect('equal')
    ax.axis('off')
    return line,

def update(frame):
    global fill
    if fill:
        fill.remove()
    scale = 1 + 0.15 * np.sin(frame * 0.3)  # 心跳缩放
    x, y = heart(t, scale)
    line.set_data(x, y)
    fill = ax.fill(x, y, color='red', alpha=0.3)[0]
    return line, fill

ani = FuncAnimation(fig, update, frames=200,
                    init_func=init, interval=30, blit=False)
plt.show()
