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 分 8.505 秒)

Gallery generated by Sphinx-Gallery