python - 如何使用 matplotlib 对 2D numpy 数组进行动画处理?

标签 python numpy matplotlib animation

我正在尝试使用 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

最佳答案

哦,谢谢,这比我想象的要容易。

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()

关于python - 如何使用 matplotlib 对 2D numpy 数组进行动画处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50413680/

相关文章:

python - 涉及 float 、整数和 sqlite 的奇怪 Python 行为

python - 如何在 pandas 索引中累积唯一的列总和

python - Numpy 切片函数 : Dynamically create slice indices np. r_[a :b, c :d, ...] 来自形状为 (X, 2) 的数组,用于在数组中进行选择

Pandas、条形图注释

python - 如何优化我的代码,以便能够使用循环在子图中绘制直方图?

python - 使用 Sunburnt+Solr 创建动态大小的 OR 查询

python - 从 MySQL 查询设置 CheckListBox

python - 在 Power BI 中使用 Python 脚本,导入错误 : Missing required dependencies ['numpy' ]

python - 使用带有 usetex=True 的 matplotlib Agg 后端导出 pdf 时找不到字体

python - GeoAlchemy2 存储点和查询结果