pyplot 動畫#

透過在繪圖指令之間呼叫 pause 來產生動畫。

此處顯示的方法僅適用於簡單、低效能的使用。對於要求更高的應用程式,請查看 animation 模組和使用它的範例。

請注意,呼叫 time.sleep 而不是 pause無法運作。

透過 matplotlib.animation.Animation.to_jshtml 產生的輸出。

frame 49
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
data = np.random.random((50, 50, 50))

fig, ax = plt.subplots()

for i, img in enumerate(data):
    ax.clear()
    ax.imshow(img)
    ax.set_title(f"frame {i}")
    # Note that using time.sleep does *not* work here!
    plt.pause(0.1)

腳本的總執行時間: (0 分鐘 11.226 秒)

由 Sphinx-Gallery 產生的圖庫