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

วิธีส่งอาร์กิวเมนต์ให้ animation.FuncAnimation() ใน Matplotlib

ในการส่งอาร์กิวเมนต์ให้ animation.FuncAnimation() สำหรับ contour plot ใน Matplotlib ใน Python เราสามารถทำตามขั้นตอนต่อไปนี้:

  • สร้างข้อมูลสุ่มขนาด 10×10
  • สร้าง figure และชุด subplots ด้วยเมธอด subplots()
  • สร้าง animation โดยการเรียกฟังก์ชัน *func* ซ้ำๆ ด้วยคลาส FuncAnimation()
  • เพื่ออัปเดตค่า contour ในฟังก์ชัน เราสามารถกำหนดเมธอด animate() ที่ใช้ในคลาส FuncAnimation()
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

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

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

data = np.random.randn(800).reshape(10, 10, 8)
fig, ax = plt.subplots()

def animate(i):
    ax.clear()
    ax.contourf(data[:, :, i])

ani = animation.FuncAnimation(fig, animate, 5, interval=50, blit=False)
plt.show()

ผลลัพธ์

วิธีส่งอาร์กิวเมนต์ให้ animation.FuncAnimation() ใน Matplotlib