fork(1) download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4.  
  5. # สร้างฟังก์ชันหัวใจ
  6. def heart(t, scale=1):
  7. x = 16 * np.sin(t) ** 3
  8. y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
  9. return scale * x, scale * y
  10.  
  11. # ตั้งค่ากราฟ
  12. fig, ax = plt.subplots(figsize=(6, 6))
  13. ax.set_xlim(-20, 20)
  14. ax.set_ylim(-20, 20)
  15. ax.set_aspect('equal')
  16. ax.axis('off')
  17.  
  18. # เตรียมข้อมูลเส้นหัวใจ
  19. t = np.linspace(0, 2 * np.pi, 1000)
  20. lines = []
  21. num_layers = 10 # จำนวนชั้นของเส้นหัวใจ
  22. colors = plt.cm.Reds(np.linspace(0.3, 1, num_layers)) # ไล่เฉดสีแดง
  23.  
  24. # สร้างเส้นหัวใจหลายชั้น
  25. for i in range(num_layers):
  26. line, = ax.plot([], [], lw=2, color=colors[i])
  27. lines.append(line)
  28.  
  29. # ฟังก์ชันอัปเดตแอนิเมชัน
  30. def update(frame):
  31. for i, line in enumerate(lines):
  32. scale = 1 + 0.1 * np.sin(2 * np.pi * (frame / 50 + i / num_layers)) # สร้างการสั่นแบบคลื่น
  33. x, y = heart(t, scale)
  34. line.set_data(x, y)
  35. return lines
  36.  
  37. # สร้างแอนิเมชัน
  38. ani = FuncAnimation(fig, update, frames=100, interval=50, blit=True)
  39.  
  40. # แสดงผล
  41. plt.show()
Success #stdin #stdout 0.98s 57808KB
stdin
Standard input is empty
stdout
Standard output is empty