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

Skip to content

Commit 8b13952

Browse files
committed
Merge pull request #1442 from maxalbert/add_savefig_kwargs_to_animation_save_method
Add savefig_kwargs to Animation.save() method
2 parents 39b5c7b + fe44357 commit 8b13952

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

lib/matplotlib/animation.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,19 @@ def finish(self):
184184
'Finish any processing for writing the movie.'
185185
self.cleanup()
186186

187-
def grab_frame(self):
187+
def grab_frame(self, **savefig_kwargs):
188188
'''
189189
Grab the image information from the figure and save as a movie frame.
190+
All keyword arguments in savefig_kwargs are passed on to the 'savefig'
191+
command that saves the figure.
190192
'''
191193
verbose.report('MovieWriter.grab_frame: Grabbing frame.',
192194
level='debug')
193195
try:
194196
# Tell the figure to save its data to the sink, using the
195197
# frame format and dpi.
196198
self.fig.savefig(self._frame_sink(), format=self.frame_format,
197-
dpi=self.dpi)
199+
dpi=self.dpi, **savefig_kwargs)
198200
except RuntimeError:
199201
out, err = self._proc.communicate()
200202
verbose.report('MovieWriter -- Error running proc:\n%s\n%s' % (out,
@@ -545,7 +547,8 @@ def _stop(self, *args):
545547
self.event_source = None
546548

547549
def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
548-
bitrate=None, extra_args=None, metadata=None, extra_anim=None):
550+
bitrate=None, extra_args=None, metadata=None, extra_anim=None,
551+
savefig_kwargs=None):
549552
'''
550553
Saves a movie file by drawing every frame.
551554
@@ -586,7 +589,32 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
586589
`matplotlib.Figure` instance. Also, animation frames will just be
587590
simply combined, so there should be a 1:1 correspondence between
588591
the frames from the different animations.
592+
593+
*savefig_kwargs* is a dictionary containing keyword arguments to be
594+
passed on to the 'savefig' command which is called repeatedly to save
595+
the individual frames. This can be used to set tight bounding boxes,
596+
for example.
589597
'''
598+
if savefig_kwargs is None:
599+
savefig_kwargs = {}
600+
601+
# FIXME: Using 'bbox_inches' doesn't currently work with writers that pipe
602+
# the data to the command because this requires a fixed frame size (see
603+
# Ryan May's reply in this thread: [1]). Thus we drop the 'bbox_inches'
604+
# argument if it exists in savefig_kwargs.
605+
#
606+
# [1] http://matplotlib.1069221.n5.nabble.com/Animation-class-let-save-accept-kwargs-which-are-passed-on-to-savefig-td39627.html
607+
#
608+
if savefig_kwargs.has_key('bbox_inches'):
609+
if not (writer in ['ffmpeg_file', 'mencoder_file'] or
610+
isinstance(writer, (FFMpegFileWriter, MencoderFileWriter))):
611+
print("Warning: discarding the 'bbox_inches' argument in " \
612+
"'savefig_kwargs' as it is only currently supported " \
613+
"with the writers 'ffmpeg_file' and 'mencoder_file' " \
614+
"(writer used: '{}').".format(writer if isinstance(writer, str)
615+
else writer.__class__.__name__))
616+
savefig_kwargs.pop('bbox_inches')
617+
590618
# Need to disconnect the first draw callback, since we'll be doing
591619
# draws. Otherwise, we'll end up starting the animation.
592620
if self._first_draw_id is not None:
@@ -645,7 +673,7 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
645673
for anim, d in zip(all_anim, data):
646674
#TODO: Need to see if turning off blit is really necessary
647675
anim._draw_next_frame(d, blit=False)
648-
writer.grab_frame()
676+
writer.grab_frame(**savefig_kwargs)
649677

650678
# Reconnect signal for first draw if necessary
651679
if reconnect_first_draw:

0 commit comments

Comments
 (0)