如何在Python中使用matplotlib.animate來對等值線圖進行動畫處理?


若要使用matplotlib 在Python中對等值線圖進行動畫處理,我們可以採取以下步驟-

  • 建立形狀為10☓10維度的隨機資料。
  • 使用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.00, 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年5月15日

655次瀏覽

開啟您的 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.