首页 > 解决方案 > 有没有办法将 matplotlib 的 FuncAnimation 向前或向后移动一帧?

问题描述

现在,FuncAnimation 只能分别使用 event_source.start() 和 event_source.stop() 启动和停止,那么有没有办法在保持 blitting 的同时将 matplotlib FuncAnimation 向前移动一帧或向后移动一帧?

下面显示的示例是 FuncAnimation 的 matplotlib 示例。理想情况下,最好有一个按钮可以将动画向前或向后移动一帧。


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

fig = plt.figure()


def f(x, y):
    return np.sin(x) + np.cos(y)

x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

im = plt.imshow(f(x, y), animated=True)


def updatefig(*args):
    global x, y
    x += np.pi / 15.
    y += np.pi / 20.
    im.set_array(f(x, y))
    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()

标签: pythonpython-3.xmatplotlibmatplotlib-basemapmatplotlib-animation

解决方案


推荐阅读