In Jupyter Lab on Windows, the matplotlib animation API documentation breaks when a no-op edit is added in the update loop.
%matplotlib ipympl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = ax.plot([], [], 'ro')
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
#if False:
# xdata = xdata[1:]
# ydata = ydata[1:]
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
plt.show()
The above is verbatim the code provided in the documentation, plus three comments in update. With the 3 lines commented, the code works; a sine wave is displayed from start to finish over small intervals.
However, when the three lines are uncommented, the animation never starts. My goal was to show a fixed-size sliding window of the function at a time. I started with the condition if len(xdata) > 10:. I learned that the condition never has to be met for the animation to break. In fact, the condition can be impossible.
The conditional statement
if len(xdata) > 10:
pass
does NOT cause the animation to fail, indicating that it's the block within the condition - not the condition itself - that is the breaking change.
I have also tried setting blit=False and removing the return statements, but the same behavior happens - the animation never starts.
Versions:
ipykernel==6.29.4
ipympl==0.9.6
ipython==8.25.0
ipywidgets==8.1.5
jupyterlab==4.3.5
matplotlib==3.10.0
numpy==1.26.4
%matplotlib ipymplto be explicit that ipympl is involved, see the documentation here. Thatwidgetversion is outdated now that JupyterLab and Jupyter Notebook use the same underlying componentes and it actually usesipymplunder the hood. It is only allowed for 'legacy' support at this point.