ในการสร้างไฟล์ GIF แบบเคลื่อนไหวจาก D3.js ภาพเคลื่อนไหว เราสามารถทำตามขั้นตอนต่อไปนี้ -
- กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
- สร้างตัวเลขใหม่หรือเปิดใช้งานตัวเลขที่มีอยู่
- เพิ่มแกนให้กับตัวเลขปัจจุบันและทำให้เป็นแกนปัจจุบัน
- พล็อตบรรทัดที่มีรายการว่าง
- ในการเริ่มต้นบรรทัด ให้ส่งรายการที่ว่างเปล่า
- เพื่อทำให้เส้นโค้งไซน์เคลื่อนไหว ให้อัปเดตค่าเส้นโค้งไซน์และคืนค่าอินสแตนซ์ของเส้น
- รับตัวอย่างนักเขียนภาพยนตร์โดยใช้ PillowWriter() คลาส.
- บันทึกไฟล์ .gif โดยใช้ PillowWriter .
ตัวอย่าง
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,
ani = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)
writer = animation.PillowWriter(fps=25)
ani.save("sine.gif", writer=writer) ผลลัพธ์
