@@ -433,7 +433,7 @@ def __init__(self, *args, **kwargs):
433433 MovieWriter .__init__ (self , * args , ** kwargs )
434434 self .frame_format = mpl .rcParams ['animation.frame_format' ]
435435
436- def setup (self , fig , outfile , dpi = None , frame_prefix = '_tmp' ,
436+ def setup (self , fig , outfile , dpi = None , frame_prefix = None ,
437437 clear_temp = True ):
438438 """
439439 Perform setup for writing the movie file.
@@ -449,13 +449,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
449449 controls the size in pixels of the resulting movie file.
450450 Default is fig.dpi.
451451 frame_prefix : str, optional
452- The filename prefix to use for temporary files. Defaults to
453- ``'_tmp'``.
452+ The filename prefix to use for temporary files. If None (the
453+ default), files are written to a temporary directory which is
454+ deleted by `cleanup` (regardless of the value of *clear_temp*).
454455 clear_temp : bool, optional
455456 If the temporary files should be deleted after stitching
456457 the final result. Setting this to ``False`` can be useful for
457458 debugging. Defaults to ``True``.
458-
459459 """
460460 self .fig = fig
461461 self .outfile = outfile
@@ -464,8 +464,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
464464 self .dpi = dpi
465465 self ._adjust_frame_size ()
466466
467+ if frame_prefix is None :
468+ self ._tmpdir = TemporaryDirectory ()
469+ self .temp_prefix = str (Path (self ._tmpdir .name , 'tmp' ))
470+ else :
471+ self ._tmpdir = None
472+ self .temp_prefix = frame_prefix
467473 self .clear_temp = clear_temp
468- self .temp_prefix = frame_prefix
469474 self ._frame_counter = 0 # used for generating sequential file names
470475 self ._temp_paths = list ()
471476 self .fname_format_str = '%s%%07d.%s'
@@ -527,13 +532,15 @@ def finish(self):
527532
528533 def cleanup (self ):
529534 MovieWriter .cleanup (self )
530-
531- # Delete temporary files
532- if self .clear_temp :
533- _log .debug ('MovieWriter: clearing temporary paths=%s' ,
534- self ._temp_paths )
535- for path in self ._temp_paths :
536- path .unlink ()
535+ if self ._tmpdir :
536+ _log .debug ('MovieWriter: clearing temporary path=%s' , self ._tmpdir )
537+ self ._tmpdir .cleanup ()
538+ else :
539+ if self .clear_temp :
540+ _log .debug ('MovieWriter: clearing temporary paths=%s' ,
541+ self ._temp_paths )
542+ for path in self ._temp_paths :
543+ path .unlink ()
537544
538545
539546@writers .register ('pillow' )
0 commit comments