@@ -184,17 +184,19 @@ def finish(self):
184
184
'Finish any processing for writing the movie.'
185
185
self .cleanup ()
186
186
187
- def grab_frame (self ):
187
+ def grab_frame (self , ** savefig_kwargs ):
188
188
'''
189
189
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.
190
192
'''
191
193
verbose .report ('MovieWriter.grab_frame: Grabbing frame.' ,
192
194
level = 'debug' )
193
195
try :
194
196
# Tell the figure to save its data to the sink, using the
195
197
# frame format and dpi.
196
198
self .fig .savefig (self ._frame_sink (), format = self .frame_format ,
197
- dpi = self .dpi )
199
+ dpi = self .dpi , ** savefig_kwargs )
198
200
except RuntimeError :
199
201
out , err = self ._proc .communicate ()
200
202
verbose .report ('MovieWriter -- Error running proc:\n %s\n %s' % (out ,
@@ -545,7 +547,8 @@ def _stop(self, *args):
545
547
self .event_source = None
546
548
547
549
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 ):
549
552
'''
550
553
Saves a movie file by drawing every frame.
551
554
@@ -586,7 +589,32 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
586
589
`matplotlib.Figure` instance. Also, animation frames will just be
587
590
simply combined, so there should be a 1:1 correspondence between
588
591
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.
589
597
'''
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
+
590
618
# Need to disconnect the first draw callback, since we'll be doing
591
619
# draws. Otherwise, we'll end up starting the animation.
592
620
if self ._first_draw_id is not None :
@@ -645,7 +673,7 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
645
673
for anim , d in zip (all_anim , data ):
646
674
#TODO: Need to see if turning off blit is really necessary
647
675
anim ._draw_next_frame (d , blit = False )
648
- writer .grab_frame ()
676
+ writer .grab_frame (** savefig_kwargs )
649
677
650
678
# Reconnect signal for first draw if necessary
651
679
if reconnect_first_draw :
0 commit comments