如何使用matplotlib动画2D numpy数组?

zvms9eto  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(91)

我正在尝试使用matplotlib动画一个numpy数组:

import numpy as np

import matplotlib.pyplot as plt
import matplotlib.animation as animation

arr = []
for i in range(100):
    c = np.random.rand(10, 10)        
    arr.append(c)

plt.imshow(arr[45])

我不知道我应该如何动画一个数组,像这样:https://matplotlib.org/examples/animation/dynamic_image.html

vhipe2zx

vhipe2zx1#

“谢谢,比我想象的要简单。

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

fig = plt.figure()
i=0
im = plt.imshow(arr[0], animated=True)
def updatefig(*args):
    global i
    if (i<99):
        i += 1
    else:
        i=0
    im.set_array(arr[i])
    return im,
ani = animation.FuncAnimation(fig, updatefig,  blit=True)
plt.show()

相关问题