如何在Matplotlib中將引數傳遞給animation.FuncAnimation()?


要在Python的Matplotlib中為等值線圖向animation.FuncAnimation()傳遞引數,我們可以執行以下步驟 -

  • 建立一個10☓10維度的隨機數。
  • 建立一個數字和一系列子圖,使用subplots()方法。
  • 使用 FuncAnimation()類反覆呼叫函式*func*來製作動畫
  • 要在函式中更新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()

輸出

更新於: 2021年7月8日

690次瀏覽量

開啟你的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.