0

When using animation.FuncAnimation, is it possible to optimally reverse an animation after it plays in forward? i.e. equivalent of

FuncAnimation(.., frames=np.hstack([range(10), range(9)[::-1]]))

This redraws frames in reverse order, eating up drawing computation and memory when 50% of data is exactly the same. FuncAnimation does have a caching mechanism, but does it or any other animation. have a reverse=True? Note all my data is pre-computed and retrieved via simple index for each frame.

OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101
  • If it is precomputed, why don't you use [ArtistAnimation](https://matplotlib.org/3.1.1/gallery/animation/dynamic_image.html#sphx-glr-gallery-animation-dynamic-image-py)? That is exactly the kind of application this flip book approach is designed for. – Mr. T Dec 08 '20 at 09:27
  • I agree with Mr T. I used artist and `framelist += framelist[-1::-1]` and it works like a charm!! – John Henckel Feb 13 '22 at 16:37

1 Answers1

1

Are you looking for a solution within the (interactive?) matplotlib backend or are you writing this animation to file?

The latter is quite a lot simpler, as you could simply use a tool like ffmpeg to revert&append the final gif/movie file. See this answer, for example.

If you need a solution within matplotlib then I guess you'd need to store all your required calculations in separate objects (e.g. create a dictionary like frames = { 0: {"x":0, "y":1, … } } and then tell the FuncAnimation to update with a simple "lookup" function like:

def update(frame):
    xdata = frames[frame]["x"]
    ydata = frames[frame]["y"]
    ln.set_data(xdata, ydata)
    return ln,

A third option would be to store your axes as pickle'd object, i.e.:

pickle.dump(ax, file('frame_0.pickle', 'w'))

and then try to re-load those pickle'd frames within FuncAnimation's update call like:

ax = pickle.load(file('frame_0.pickle'))
Asmus
  • 5,117
  • 1
  • 16
  • 21