diff --git a/doc/api/next_api_changes/behaviour.rst b/doc/api/next_api_changes/behaviour.rst index 9796a64ad84f..330f67c29b94 100644 --- a/doc/api/next_api_changes/behaviour.rst +++ b/doc/api/next_api_changes/behaviour.rst @@ -56,3 +56,9 @@ Setting the same property under multiple aliases now raises a TypeError Previously, calling e.g. ``plot(..., color=somecolor, c=othercolor)`` would emit a warning because ``color`` and ``c`` actually map to the same Artist property. This now raises a TypeError. + +`.FileMovieWriter` temporary frames directory +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +`.FileMovieWriter` now defaults to writing temporary frames in a temporary +directory, which is always cleared at exit. In order to keep the individual +frames saved on the filesystem, pass an explicit *frame_prefix*. diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 340596d567bd..4e6027bedcc8 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -433,7 +433,7 @@ def __init__(self, *args, **kwargs): MovieWriter.__init__(self, *args, **kwargs) self.frame_format = mpl.rcParams['animation.frame_format'] - def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp', + def setup(self, fig, outfile, dpi=None, frame_prefix=None, clear_temp=True): """ Perform setup for writing the movie file. @@ -449,13 +449,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp', controls the size in pixels of the resulting movie file. Default is fig.dpi. frame_prefix : str, optional - The filename prefix to use for temporary files. Defaults to - ``'_tmp'``. + The filename prefix to use for temporary files. If None (the + default), files are written to a temporary directory which is + deleted by `cleanup` (regardless of the value of *clear_temp*). clear_temp : bool, optional If the temporary files should be deleted after stitching the final result. Setting this to ``False`` can be useful for debugging. Defaults to ``True``. - """ self.fig = fig self.outfile = outfile @@ -464,8 +464,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp', self.dpi = dpi self._adjust_frame_size() + if frame_prefix is None: + self._tmpdir = TemporaryDirectory() + self.temp_prefix = str(Path(self._tmpdir.name, 'tmp')) + else: + self._tmpdir = None + self.temp_prefix = frame_prefix self.clear_temp = clear_temp - self.temp_prefix = frame_prefix self._frame_counter = 0 # used for generating sequential file names self._temp_paths = list() self.fname_format_str = '%s%%07d.%s' @@ -527,13 +532,15 @@ def finish(self): def cleanup(self): MovieWriter.cleanup(self) - - # Delete temporary files - if self.clear_temp: - _log.debug('MovieWriter: clearing temporary paths=%s', - self._temp_paths) - for path in self._temp_paths: - path.unlink() + if self._tmpdir: + _log.debug('MovieWriter: clearing temporary path=%s', self._tmpdir) + self._tmpdir.cleanup() + else: + if self.clear_temp: + _log.debug('MovieWriter: clearing temporary paths=%s', + self._temp_paths) + for path in self._temp_paths: + path.unlink() @writers.register('pillow')