Skip to content

Commit 949ecdd

Browse files
committed
MNT: Refactor animation handling of 'bbox_inches'.
Instead of a hard-coded look for either instance types or strings, make it a property of the MovieWriter instances. Set the flag to true by default for the MovieFileWriters (which all seem to work fine) and make it false by default for MovieWriters (which are currently pipe-based and break). We can also eliminate looking at strings by doing the check after we create the instance of MovieWriter (if necessary).
1 parent 1156e23 commit 949ecdd

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

lib/matplotlib/animation.py

+28-24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from base64 import encodestring as encodebytes
3636
import contextlib
3737
import tempfile
38+
import warnings
3839
from matplotlib.cbook import iterable, is_string_like
3940
from matplotlib.compat import subprocess
4041
from matplotlib import verbose
@@ -109,6 +110,11 @@ class MovieWriter(object):
109110
frame_format: string
110111
The format used in writing frame data, defaults to 'rgba'
111112
'''
113+
114+
# Specifies whether the size of all frames need to be identical
115+
# i.e. whether we can use savefig.bbox = 'tight'
116+
frame_size_can_vary = False
117+
112118
def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
113119
metadata=None):
114120
'''
@@ -283,6 +289,11 @@ def isAvailable(cls):
283289

284290
class FileMovieWriter(MovieWriter):
285291
'`MovieWriter` subclass that handles writing to a file.'
292+
293+
# In general, if frames are writen to files on disk, it's not important
294+
# that they all be identically sized
295+
frame_size_can_vary = True
296+
286297
def __init__(self, *args, **kwargs):
287298
MovieWriter.__init__(self, *args, **kwargs)
288299
self.frame_format = rcParams['animation.frame_format']
@@ -712,29 +723,6 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
712723
if savefig_kwargs is None:
713724
savefig_kwargs = {}
714725

715-
# FIXME: Using 'bbox_inches' doesn't currently work with
716-
# writers that pipe the data to the command because this
717-
# requires a fixed frame size (see Ryan May's reply in this
718-
# thread: [1]). Thus we drop the 'bbox_inches' argument if it
719-
# exists in savefig_kwargs.
720-
#
721-
# [1] (http://matplotlib.1069221.n5.nabble.com/
722-
# Animation-class-let-save-accept-kwargs-which-
723-
# are-passed-on-to-savefig-td39627.html)
724-
#
725-
if 'bbox_inches' in savefig_kwargs:
726-
if not (writer in ['ffmpeg_file', 'mencoder_file'] or
727-
isinstance(writer,
728-
(FFMpegFileWriter, MencoderFileWriter))):
729-
print("Warning: discarding the 'bbox_inches' argument in "
730-
"'savefig_kwargs' as it is only currently supported "
731-
"with the writers 'ffmpeg_file' and 'mencoder_file' "
732-
"(writer used: "
733-
"'{0}').".format(
734-
writer if isinstance(writer, six.string_types)
735-
else writer.__class__.__name__))
736-
savefig_kwargs.pop('bbox_inches')
737-
738726
# Need to disconnect the first draw callback, since we'll be doing
739727
# draws. Otherwise, we'll end up starting the animation.
740728
if self._first_draw_id is not None:
@@ -778,7 +766,6 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
778766
extra_args=extra_args,
779767
metadata=metadata)
780768
else:
781-
import warnings
782769
warnings.warn("MovieWriter %s unavailable" % writer)
783770

784771
try:
@@ -792,6 +779,23 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
792779

793780
verbose.report('Animation.save using %s' % type(writer),
794781
level='helpful')
782+
783+
# FIXME: Using 'bbox_inches' doesn't currently work with
784+
# writers that pipe the data to the command because this
785+
# requires a fixed frame size (see Ryan May's reply in this
786+
# thread: [1]). Thus we drop the 'bbox_inches' argument if it
787+
# exists in savefig_kwargs.
788+
#
789+
# [1] (http://matplotlib.1069221.n5.nabble.com/
790+
# Animation-class-let-save-accept-kwargs-which-
791+
# are-passed-on-to-savefig-td39627.html)
792+
#
793+
if 'bbox_inches' in savefig_kwargs and not writer.frame_size_can_vary:
794+
warnings.warn("Warning: discarding the 'bbox_inches' argument in "
795+
"'savefig_kwargs' as it not supported by "
796+
"{0}).".format(writer.__class__.__name__))
797+
savefig_kwargs.pop('bbox_inches')
798+
795799
# Create a new sequence of frames for saved data. This is different
796800
# from new_frame_seq() to give the ability to save 'live' generated
797801
# frame information to be saved later.

0 commit comments

Comments
 (0)