Skip to content

Commit f6dc4e0

Browse files
fredrik-1QuLogic
authored andcommitted
Pass AbstractMovieWriter.setup kwargs through Animation.save
1 parent 6083ecd commit f6dc4e0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/matplotlib/animation.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def saving(self, fig, outfile, dpi, *args, **kwargs):
210210
"""
211211
Context manager to facilitate writing the movie file.
212212
213-
``*args, **kw`` are any parameters that should be passed to `setup`.
213+
``*args, **kwargs`` are any parameters that should be passed to `setup`.
214214
"""
215215
if mpl.rcParams['savefig.bbox'] == 'tight':
216216
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
@@ -939,7 +939,7 @@ def _stop(self, *args):
939939

940940
def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
941941
bitrate=None, extra_args=None, metadata=None, extra_anim=None,
942-
savefig_kwargs=None, *, progress_callback=None):
942+
savefig_kwargs=None, *, progress_callback=None, **kwargs):
943943
"""
944944
Save the animation as a movie file by drawing every frame.
945945
@@ -1006,6 +1006,11 @@ def func(current_frame: int, total_frames: int) -> Any
10061006
10071007
progress_callback = lambda i, n: print(f'Saving frame {i}/{n}')
10081008
1009+
**kwargs :
1010+
`AbstractMovieWriter` subclasses can specify additional parameters in their
1011+
`~.AbstractMovieWriter.setup` methods. Additional keyword arguments
1012+
are passed to these setup functions.
1013+
10091014
Notes
10101015
-----
10111016
*fps*, *codec*, *bitrate*, *extra_args* and *metadata* are used to
@@ -1092,7 +1097,7 @@ def _pre_composite_to_white(color):
10921097
# canvas._is_saving = True makes the draw_event animation-starting
10931098
# callback a no-op; canvas.manager = None prevents resizing the GUI
10941099
# widget (both are likewise done in savefig()).
1095-
with (writer.saving(self._fig, filename, dpi),
1100+
with (writer.saving(self._fig, filename, dpi, **kwargs),
10961101
cbook._setattr_cm(self._fig.canvas, _is_saving=True, manager=None)):
10971102
if not writer._supports_transparency():
10981103
facecolor = savefig_kwargs.get('facecolor',

lib/matplotlib/tests/test_animation.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ class NullMovieWriter(animation.AbstractMovieWriter):
5656
it cannot be added to the 'writers' registry.
5757
"""
5858

59-
def setup(self, fig, outfile, dpi, *args):
59+
def setup(self, fig, outfile, dpi, *args, **kwargs):
6060
self.fig = fig
6161
self.outfile = outfile
6262
self.dpi = dpi
6363
self.args = args
64+
self.kwargs = kwargs
6465
self._count = 0
6566

6667
def grab_frame(self, **savefig_kwargs):
@@ -79,15 +80,17 @@ def test_null_movie_writer(anim):
7980
filename = "unused.null"
8081
dpi = 50
8182
savefig_kwargs = dict(foo=0)
83+
setup_kwargs = dict(bar=1)
8284
writer = NullMovieWriter()
8385

8486
anim.save(filename, dpi=dpi, writer=writer,
85-
savefig_kwargs=savefig_kwargs)
87+
savefig_kwargs=savefig_kwargs, **setup_kwargs)
8688

8789
assert writer.fig == plt.figure(1) # The figure used by anim fixture
8890
assert writer.outfile == filename
8991
assert writer.dpi == dpi
9092
assert writer.args == ()
93+
assert writer.kwargs == setup_kwargs
9194
# we enrich the savefig kwargs to ensure we composite transparent
9295
# output to an opaque background
9396
for k, v in savefig_kwargs.items():
@@ -187,8 +190,18 @@ def test_save_animation_smoketest(tmp_path, writer, frame_format, output, anim):
187190
dpi = 100.
188191
codec = 'h264'
189192

193+
setup_kwargs = {}
194+
if writer.endswith('_file'):
195+
assert len([*tmp_path.glob('example*')]) == 0
196+
# Test that kwargs are passed to `MovieFileWriter.setup` so that the automatic
197+
# temporary directory isn't used.
198+
setup_kwargs['frame_prefix'] = str(tmp_path / 'example')
199+
190200
anim.save(tmp_path / output, fps=30, writer=writer, bitrate=500, dpi=dpi,
191-
codec=codec)
201+
codec=codec, **setup_kwargs)
202+
203+
if writer.endswith('_file'):
204+
assert len([*tmp_path.glob('example*')]) == anim._save_count
192205

193206
del anim
194207

0 commit comments

Comments
 (0)