Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2874ba0

Browse files
committed
Defaut to writing animation frames to a temporary directory.
(We could later deprecate clean_temp, as the user can simply pass a non-None frame_prefix to write temporary frames whereever they want and keep them there.)
1 parent 745dcae commit 2874ba0

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ Setting the same property under multiple aliases now raises a TypeError
5656
Previously, calling e.g. ``plot(..., color=somecolor, c=othercolor)`` would
5757
emit a warning because ``color`` and ``c`` actually map to the same Artist
5858
property. This now raises a TypeError.
59+
60+
`.FileMovieWriter` temporary frames directory
61+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62+
`.FileMovieWriter` now defaults to writing temporary frames in a temporary
63+
directory, which is always cleared at exit. In order to keep the individual
64+
frames saved on the filesystem, pass an explicit *frame_prefix*.

lib/matplotlib/animation.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def __init__(self, *args, **kwargs):
431431
MovieWriter.__init__(self, *args, **kwargs)
432432
self.frame_format = mpl.rcParams['animation.frame_format']
433433

434-
def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
434+
def setup(self, fig, outfile, dpi=None, frame_prefix=None,
435435
clear_temp=True):
436436
'''Perform setup for writing the movie file.
437437
@@ -446,13 +446,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
446446
controls the size in pixels of the resulting movie file.
447447
Default is fig.dpi.
448448
frame_prefix : str, optional
449-
The filename prefix to use for temporary files. Defaults to
450-
``'_tmp'``.
449+
The filename prefix to use for temporary files. If None (the
450+
default), files are written to a temporary directory which is
451+
deleted by `cleanup` (regardless of the value of *clear_temp*).
451452
clear_temp : bool, optional
452453
If the temporary files should be deleted after stitching
453454
the final result. Setting this to ``False`` can be useful for
454455
debugging. Defaults to ``True``.
455-
456456
'''
457457
self.fig = fig
458458
self.outfile = outfile
@@ -461,8 +461,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
461461
self.dpi = dpi
462462
self._adjust_frame_size()
463463

464+
if frame_prefix is None:
465+
self._tmpdir = TemporaryDirectory()
466+
self.temp_prefix = str(Path(self._tmpdir.name, 'tmp'))
467+
else:
468+
self._tmpdir = None
469+
self.temp_prefix = frame_prefix
464470
self.clear_temp = clear_temp
465-
self.temp_prefix = frame_prefix
466471
self._frame_counter = 0 # used for generating sequential file names
467472
self._temp_paths = list()
468473
self.fname_format_str = '%s%%07d.%s'
@@ -524,13 +529,15 @@ def finish(self):
524529

525530
def cleanup(self):
526531
MovieWriter.cleanup(self)
527-
528-
# Delete temporary files
529-
if self.clear_temp:
530-
_log.debug('MovieWriter: clearing temporary paths=%s',
531-
self._temp_paths)
532-
for path in self._temp_paths:
533-
path.unlink()
532+
if self._tmpdir:
533+
_log.debug('MovieWriter: clearing temporary path=%s', self._tmpdir)
534+
self._tmpdir.cleanup()
535+
else:
536+
if self.clear_temp:
537+
_log.debug('MovieWriter: clearing temporary paths=%s',
538+
self._temp_paths)
539+
for path in self._temp_paths:
540+
path.unlink()
534541

535542

536543
@writers.register('pillow')

0 commit comments

Comments
 (0)