You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FuncAnimation gets only triggered/executed when it is assigned. For example _ = animation.FuncAnimation(*params) and anim = animation.FuncAnimation(*params) work, whereas animation.FuncAnimation(*params) does not work.
Code for reproduction
An example is the decay example from the matplotlib homepage.
I just changed ani = animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init) to animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init)
importitertoolsimportnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlib.animationasanimationdefdata_gen():
forcntinitertools.count():
t=cnt/10yieldt, np.sin(2*np.pi*t) *np.exp(-t/10.)
definit():
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 10)
delxdata[:]
delydata[:]
line.set_data(xdata, ydata)
returnline,
fig, ax=plt.subplots()
line, =ax.plot([], [], lw=2)
ax.grid()
xdata, ydata= [], []
defrun(data):
# update the datat, y=dataxdata.append(t)
ydata.append(y)
xmin, xmax=ax.get_xlim()
ift>=xmax:
ax.set_xlim(xmin, 2*xmax)
ax.figure.canvas.draw()
line.set_data(xdata, ydata)
returnline,
animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init) # This is the only line different from the original exampleplt.show()
Actual outcome
The animation does not start.
Expected outcome
The animation starts.
This problem only occurs for matplotlib==3.3.x. With matplotlib==3.2.2 this issue does not occur.
In both cases it is critical to keep a reference to the instance object. The animation is advanced by a timer (typically from the host GUI framework) which the Animation object holds the only reference to. If you do not hold a reference to the Animation object, it (and hence the timers), will be garbage collected which will stop the animation.
However that might not be very discoverable. We could additionally mention it in the class docstring and/or the examples themselves.
Activity
jklymak commentedon Nov 4, 2020
I believe that is on purpose to avoid garbage collection of the animation.
timhoffm commentedon Nov 4, 2020
It's documented here: https://matplotlib.org/devdocs/api/animation_api.html:
However that might not be very discoverable. We could additionally mention it in the class docstring and/or the examples themselves.
HaaLeo commentedon Nov 4, 2020
@timhoffm thx for clarification 👍🏼 . I only searched the changelog of v3.3.0 and could not find anything.
Please feel free to close the issue then if you decide to not take further action.