Skip to content

Use collections.deque to store animation cache data. #30206

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

Merged
merged 1 commit into from
Jun 24, 2025
Merged
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
11 changes: 5 additions & 6 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import abc
import base64
import collections
import contextlib
from io import BytesIO, TextIOWrapper
import itertools
Expand Down Expand Up @@ -1708,13 +1709,13 @@
self._cache_frame_data = cache_frame_data

# Needs to be initialized so the draw functions work without checking
self._save_seq = []
self._save_seq = collections.deque([], self._save_count)

super().__init__(fig, **kwargs)

# Need to reset the saved seq, since right now it will contain data
# for a single frame from init, which is not what we want.
self._save_seq = []
self._save_seq.clear()

def new_frame_seq(self):
# Use the generating function to generate a new frame sequence
Expand All @@ -1727,8 +1728,7 @@
if self._save_seq:
# While iterating we are going to update _save_seq
# so make a copy to safely iterate over
self._old_saved_seq = list(self._save_seq)
return iter(self._old_saved_seq)
return iter([*self._save_seq])

Check warning on line 1731 in lib/matplotlib/animation.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/animation.py#L1731

Added line #L1731 was not covered by tests
else:
if self._save_count is None:
frame_seq = self.new_frame_seq()
Expand Down Expand Up @@ -1773,13 +1773,12 @@
'return a sequence of Artist objects.')
for a in self._drawn_artists:
a.set_animated(self._blit)
self._save_seq = []
self._save_seq.clear()

def _draw_frame(self, framedata):
if self._cache_frame_data:
# Save the data for potential saving of movies.
self._save_seq.append(framedata)
self._save_seq = self._save_seq[-self._save_count:]

# Call the func with framedata and args. If blitting is desired,
# func needs to return a sequence of any artists that were modified.
Expand Down
Loading