I’m trying to run an animation where a scatter plot is updated every timestep. I am working in a jupyter notebook in vscode (if relevant). The animation script, given below, runs great the first time but it doesn’t execute as intended on subsequent runs. The problem is only fixed when I restart the kernel. My work requires me to constantly modify this animation code and hence need to rerun it multiple times. Restarting the kernel every time is cumbersome.
In following code, I am plotting object locations (block_centroids
) using a scatter plot and updating them using the displacements at time timepoints[i]
(given by fields[i]
). The index i (val) increases in steps of 50 (tstep
). The last time point is 5450 ms.
import matplotlib
matplotlib.use('Agg')
%matplotlib widget
import matplotlib.pyplot as plt
from matplotlib import animation
fig, axes = plt.subplots(figsize=(9,5), tight_layout = True)
tstep = 50
xs, ys = block_centroids.T
scat = axes.scatter(xs, ys)
axes.set_aspect('equal', adjustable='box')
def update(val):
if val == len(timepoints)//tstep - 1:
print('closing!')
exit()
scat.set_offsets(block_centroids + fields[tstep*val])
fig.suptitle(fr'time $t = {timepoints[tstep*val]:.2f}$ sec')
fig.canvas.draw_idle()
return scat
def init():
pass
ani = animation.FuncAnimation(fig=fig, func=update, init_func=init, frames=len(timepoints)//tstep, blit = True, interval=tstep)
After the first time I run this code block, the re-run either gets stuck at some midway time (between 0s and 5.45s) and prematurely prints ‘closing!’, or the code keeps running seemingly forever with no output and no error until I try to stop it and the kernel times out.
I have tried multiple things without completely understanding why they might work. My guess is that the animation-related objects are not reset and somehow the update()
function gets called with the wrong (old) value and my attempts have mostly tried to close/delete the old instances.
-
I added the
if
statement inside theupdate()
function in an attempt to close the animation after a run. Earlier, instead of theexit()
function, I was usingplt.close(fig)
which led to the animation getting stuck at a midway time and printing ‘closing!’ output repeatedly. -
I tried adding
plt.cla()
,plt.clf()
andplt.close()
at the start of the code block to close any open instances of fig and axes but that didn’t help. -
I tried deleting the
fig
andani
objects usingdel
command to remove their previous instances in hopes of getting a fresh start for every run but that didn’t work. -
Earlier even the first runs of the animation were having problems. I added
Agg
backend after reading a stackoverflow answer recommendingGTKAgg
for handling animations better. I couldn’t useGTKAgg
because I don’t havecairo
, so I settled for the closest thing and that helped is making the first run successful.
What is not working here as intended? What can I try to debug this? I tried printing val
at every function call, to check what is happening during reruns but then the code kept running without any output.
gargantuar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.