使用 Matplotlib 與子圖和 ArtistAnimation 的動畫


如需使用 Matplotlib 和子圖與 ArtistAnimation 建立動畫,我們可以執行以下步驟:

  • 設定圖形尺寸,調整子圖之間和周圍的填充。
  • 建立圖形和一組子圖。
  • 建立一個使用者自定義函式 Init 以繪製一個清晰的幀。
  • 使用 FuncAnimation 透過反覆呼叫函式 *func* 建立動畫。
  • 定義一個 animate 函式以更新 FuncArtist 類中的資料點。
  • 要顯示圖形,請使用 show() 方法。

示例

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

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'r*')

def init():
   ax.set_xlim(0, 100)
   ax.set_ylim(-1, 1)
   return ln,
def animate(frame):
   xdata.append(frame)
   ydata.append(np.sin(frame))
   ln.set_data(xdata, ydata)
   return ln,

ani = FuncAnimation(fig, animate, init_func=init, blit=True, frames=100)
plt.show()

輸出

更新時間: 2021 年 6 月 17 日

2K+ 檢視次數

開啟您的 事業

完成課程以獲得認證

入門
廣告
© . All rights reserved.