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
The animation documentation states that the reference to the Animation object must be held to prevent the animation from stopping due to garbage collection. I cannot reproduce this. Also, I do not find any way to stop the animation. I presume the Animation object is held somewhere, and this is undocumented.
The bigger picture for me: I pass as the frames argument of FuncAnimation a generator which streams data from a device via a socket. When the generator is garbage collected, the socket is deleted and the device is free to take new connections. Because I cannot garbage collect the Animation object, I cannot garbe collec the socket instrument either and the device hangs.
After more thought on it, I found a way to stop the animation which does not rely on the garbage collection of the Animation object (see code below for the record).
The behaviour described in the first message of this issue is not anymore an issue for me. Up to you to leave the issue open.
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
(ln,) = plt.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))
ln.set_data(xdata, ydata)
return (ln,)
def generate_data():
stop_flag = False
x = 0
while not stop_flag:
x += 2 * np.pi / 128
stop_flag = yield x
print("stopped")
data_generator = generate_data()
def stop():
data_generator.send("STOP")
def onClose(event):
stop()
def onPress(event):
if event.key == " ":
stop()
fig.canvas.mpl_connect("close_event", onClose)
fig.canvas.mpl_connect("key_press_event", onPress)
ani = FuncAnimation(fig, update, frames=data_generator, init_func=init, blit=False)
plt.show()
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Activity
ImportanceOfBeingErnest commentedon Mar 20, 2020
Without having looked at this too much in detail, it seems to come from blitting being used. Would
blit=Falseshow the expected behaviour?nbud commentedon Mar 20, 2020
@ImportanceOfBeingErnest Nice catch,
blit=Falseshows the expected behaviour.nbud commentedon Mar 20, 2020
After more thought on it, I found a way to stop the animation which does not rely on the garbage collection of the Animation object (see code below for the record).
The behaviour described in the first message of this issue is not anymore an issue for me. Up to you to leave the issue open.
ImportanceOfBeingErnest commentedon Mar 20, 2020
The culprit is indeed this line in
_setup_blit:matplotlib/lib/matplotlib/animation.py
Lines 1240 to 1241 in 537a8c9
github-actions commentedon Jul 14, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
tacaswell commentedon Jul 14, 2023
mpl_connectshould only be holding a weakref. This warrants more investigation.anntzer commentedon Sep 14, 2023
This seems to be fixed as of master for me; @tacaswell can you confirm or infirm this?