在 matplotlib 中使用等值線進行動畫


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

步驟

  • 設定 figure 大小並調整子圖之間及子圖周圍的邊距。

  • 為等值線圖建立資料。

  • 建立一個 figure 和一組子圖。

  • 透過反覆呼叫函式 *animate* 生成動畫,其中 animate() 方法更改等值線資料點。

  • 若要顯示 figure,請使用 Show() 方法。

示例

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

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Random data for the contour plot
data = np.random.randn(800).reshape(10, 10, 8)

# Create a figure and a set of subplots
fig, ax = plt.subplots()

# Method to change the contour data points
def animate(i):
    ax.clear()
    ax.contourf(data[:, :, i], cmap='plasma')

# Call animate method
ani = animation.FuncAnimation(fig, animate, 5, interval=50, blit=False)

# Display the plot
plt.show()

輸出

它將產生以下輸出 −

更新於: 2021-10-19

3K+ 瀏覽數

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.