fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4.  
  5. fig, ax = plt.subplots(figsize=(6, 6))
  6.  
  7. def heart(t, scale=1):
  8. x = 16 * np.sin(t) ** 3 * scale
  9. y = (13 * np.cos(t) - 5 * np.cos(2 * t)
  10. - 2 * np.cos(3 * t) - np.cos(4 * t)) * scale
  11. return x, y
  12.  
  13. t = np.linspace(0, 2 * np.pi, 500)
  14. line, = ax.plot([], [], color='red', linewidth=3)
  15. fill = None
  16.  
  17. def init():
  18. ax.set_xlim(-20, 20)
  19. ax.set_ylim(-20, 20)
  20. ax.set_aspect('equal')
  21. ax.axis('off')
  22. return line,
  23.  
  24. def update(frame):
  25. global fill
  26. if fill:
  27. fill.remove()
  28. scale = 1 + 0.15 * np.sin(frame * 0.3) # 心跳缩放
  29. x, y = heart(t, scale)
  30. line.set_data(x, y)
  31. fill = ax.fill(x, y, color='red', alpha=0.3)[0]
  32. return line, fill
  33.  
  34. ani = FuncAnimation(fig, update, frames=200,
  35. init_func=init, interval=30, blit=False)
  36. plt.show()
  37.  
Success #stdin #stdout 0.67s 55176KB
stdin
Standard input is empty
stdout
Standard output is empty