Sorry, I didn’t have time the other day to explore this better…
That static graphic corresponds to the last frame generated.
The more permanent solution is to close the current active plot view so that modern Jupyter doesn’t detect that active plot object and display it.
In practical terms for your example code, that means adding plt.close()
in the plot generating code cell just before you call the display of the playback widget that you made for stepping through the calculated frames.
Here is your code adapted to do that by adding that one line:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from IPython.display import HTML
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.1)
line, = ax.plot(x, np.sin(x))
z = x.size
def animate(i):
line.set_ydata(np.sin(x - 2*np.pi*i / z))
return line,
ani = animation.FuncAnimation(
fig, animate,
frames = z,
blit=True)
plt.close()
HTML(ani.to_jshtml())
Related examples:
- here
- here
- top example here
- Plus, note that this is related in a way to the fact you no longer need
plt.show()
in modern Jupyter, even though most examples still include it.
Glad you asked because it prompted me to update my animated_matplotlib_binder for where I show the examples generating the playback controller widget that lets you step through the animation frames.