2

I have to set blit = true, since the plotting is much faster. But after animation (repeat = false), if I use zoom in the figure, the figure will just disappear. I need to keep the last frame so that I can zoom in the last figure.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • I'm currently not sure about the reason, but I filed [an issue](https://github.com/matplotlib/matplotlib/issues/12528) for this not to be forgotten. – ImportanceOfBeingErnest Oct 14 '18 at 22:52
  • Thanks! I am thinking some tricks to "get around" this issue, like plotting only the last frame again, after the animation finishes. – Cheng Shanbao Oct 15 '18 at 16:00
  • I ran into the same problem and as a workaround I modified the frames generator that is passed to FuncAnimation to keep yielding the last element infinitely. – oh54 Aug 02 '19 at 13:19

1 Answers1

1

One work around is to initialize the animation using the last frame. The obvious down side is you have to precompute the last frame. Adapting this example would be

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

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

cnt = 50 # Define so we know what the last frame will be.

def init():
    # Note the function is the same as `animate` with `i` set to the last value
    line.set_ydata(np.sin(x + cnt / 100))
    return line,

def animate(i):
    line.set_ydata(np.sin(x + i / 100))  # update the data.
    return line,

ani = animation.FuncAnimation(
    fig, animate, init_func=init, interval=2, blit=True, save_count=cnt)

ani.save("mwe.mov")
fig.savefig("mwe.png")
Keith Prussing
  • 803
  • 8
  • 19