Matplotlib 動畫在 IPython Notebook 中不起作用?


要在 matplotlib 中製作動畫,可以採取以下步驟:-

  • 設定影像大小並調整子影像之間的邊距和周圍的邊距。

  • 建立形狀為 10X10 維度的隨機資料。

  • 使用subplots()方法建立一張圖和一組子影像。

  • 使用 FuncAnimation() 類,透過反覆呼叫函式*func*,製作動畫。

  • 要在函式中更新輪廓值,我們可以定義一種方法 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()

輸出

更新於: 16-Jun-2021

2K+ 瀏覽量

開始你的 職業生涯

透過完成該課程獲得認證

立即開始
廣告