I copied code from the manual:
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
fig, ax = plt.subplots()
t = np.linspace(0, 3, 40)
g = -9.81
v0 = 12
z = g * t**2 / 2 + v0 * t
v02 = 5
z2 = g * t**2 / 2 + v02 * t
scat = ax.scatter(t[0], z[0], c="b", s=5, label=f'v0 = {v0} m/s')
line2 = ax.plot(t[0], z2[0], label=f'v0 = {v02} m/s')[0]
ax.set(xlim=[0, 3], ylim=[-4, 10], xlabel='Time [s]', ylabel='Z [m]')
ax.legend()
def update(frame):
# for each frame, update the data stored on each artist.
x = t[:frame]
y = z[:frame]
# update the scatter plot:
data = np.stack([x, y]).T
scat.set_offsets(data)
# update the line plot:
line2.set_xdata(t[:frame])
line2.set_ydata(z2[:frame])
return (scat, line2)
ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
plt.show()
into a new notebook and run it.
Alas, all I see is the figure:
(and the cell is marked with *
).
When I restart the kernel, sometimes I get the beginning of the animation:
but I never get the controls present in the matplotlib manual.
Server Information:
You are using Jupyter Notebook.
The version of the notebook server is: 6.5.4-762f3618
The server is running on this version of Python:
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
Current Kernel Information:
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
I can save the animation in an mp4
file and view it, but I would much prefer the interactive facility.
What am I doing wrong?