Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้างอนิเมชันเส้นไซน์ (Sine Curve) ใน Matplotlib


ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่
  • เพิ่ม axes เข้าไปใน figure ปัจจุบันและทำให้เป็น axes ปัจจุบัน
  • พล็อตเส้นด้วยรายการว่าง
  • เริ่มต้นเส้นด้วยการส่งรายการว่าง
  • อนิเมชันเส้นไซน์ด้วยการอัปเดตค่าและคืนค่า line instance
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

ตัวอย่างโค้ด

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

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)
plt.show()

ผลลัพธ์

วิธีสร้างอนิเมชันเส้นไซน์ (Sine Curve) ใน Matplotlib