Skip to content

Fixed 1656: Animation is Garbage Collected if not explicitly stored #8214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,10 @@ def __init__(self, fig, event_source=None, blit=False):
if self._blit:
self._setup_blit()

# Saves the animation is the figure class to prevent Python's garbage
# collection from deleteing the animation.
fig._keep_alives.append(self)

def _start(self, *args):
'''
Starts interactive animation. Adds the draw frame command to the GUI
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def __init__(self,
self._axstack = AxesStack() # track all figure axes and current axes
self.clf()
self._cachedRenderer = None
self._keep_alives = []

@cbook.deprecated("2.1", alternative="Figure.patch")
def figurePatch(self):
Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ def animate(i):
except UnicodeDecodeError:
pytest.xfail("There can be errors in the numpy import stack, "
"see issues #1891 and #2679")
length = len(fig._keep_alives)
assert(length != 0)


def test_animation_on_save():
fig = plt.figure()

def update_line(num, data, line):
line.set_data(data[..., :num])
return line,

# Fixing random state for reproducibility
np.random.seed(19680801)

data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
animation.FuncAnimation(fig, update_line,
25, fargs=(data, l), interval=50, blit=True)
length = len(fig._keep_alives)
assert(length != 0)


def test_no_length_frames():
Expand Down