สร้าง Animation 3D โดยใช้ mpl_toolkits.mplot3d.Axes3D และ matplotlib.animation.FuncAnimation
ขั้นตอน
- Import:
Axes3D,animation - สร้างข้อมูล Parametric: t, x=cos(t)-1, y=0.5*(cos(2t)-1), z=t
- สร้าง Figure และ 3D Axes
- ปิด Axis:
ax.axis('off') - Plot เส้นเริ่มต้น
- กำหนดฟังก์ชัน
animate(num, data, line)อัปเดตข้อมูลและสี - สร้าง Animation:
FuncAnimation(fig, animate, frames=N, fargs=(data, line), interval=50) plt.show()
ตัวอย่างโค้ด
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
def animate(num, data, line):
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',
'#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
line.set_color(colors[num % len(colors)])
line.set_alpha(0.7)
line.set_data(data[0:2, :num])
line.set_3d_properties(data[2, :num])
return line
t = np.arange(0, 20, 0.2)
x = np.cos(t) - 1
y = 1 / 2 * (np.cos(2 * t) - 1)
data = np.array([x, y, t])
N = len(t)
fig = plt.figure()
ax = Axes3D(fig)
ax.axis('off')
line, = plt.plot(data[0], data[1], data[2], lw=7, c='red')
line_ani = animation.FuncAnimation(fig, animate, frames=N,
fargs=(data, line), interval=50, blit=False)
plt.show()
คำอธิบาย
set_data(x, y)และset_3d_properties(z)อัปเดตพิกัดเส้นframes=Nจำนวนเฟรม = จำนวนจุดข้อมูลinterval=50ms ต่อเฟรม (20 FPS)- สีเปลี่ยนทุกเฟรมตาม List Colors
- บันทึกเป็น GIF:
line_ani.save('3d.gif', writer='pillow')