Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ def setup(self, fig, outfile, dpi=None):
@property
def frame_size(self):
"""A tuple ``(width, height)`` in pixels of a movie frame."""
# We cannot query the canvas for width/height because the dpi may be different
# The tolerance of 1e-8 covers a floating-point tick for even 100,000 pixels
w, h = self.fig.get_size_inches()
return int(w * self.dpi), int(h * self.dpi)
return int(w * self.dpi + 1e-8), int(h * self.dpi + 1e-8)

def _supports_transparency(self):
"""
Expand Down Expand Up @@ -293,15 +295,17 @@ def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
self.extra_args = extra_args

def _adjust_frame_size(self):
wo, ho = self.frame_size # in pixels, so need to convert to inches
wo /= self.dpi
ho /= self.dpi
if self.codec == 'h264':
wo, ho = self.fig.get_size_inches()
w, h = adjusted_figsize(wo, ho, self.dpi, 2)
if (wo, ho) != (w, h):
self.fig.set_size_inches(w, h, forward=True)
_log.info('figure size in inches has been adjusted '
'from %s x %s to %s x %s', wo, ho, w, h)
else:
w, h = self.fig.get_size_inches()
w, h = wo, ho
_log.debug('frame size in pixels is %s x %s', *self.frame_size)
return w, h

Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ def test_null_movie_writer(anim):
assert writer._count == anim._save_count


def test_frame_size():
# Test that the frame size is the canvas size and not the figure size
fig = plt.figure(figsize=(1, 2.03), dpi=100)
assert fig.bbox.height < 203 # due to floating-point precision
assert fig.canvas.get_width_height() == (100, 203)

anim = animation.FuncAnimation(fig, lambda frame: tuple(), frames=1)
writer = NullMovieWriter()
anim.save("unused.null", dpi=100, writer=writer)

assert writer.frame_size == fig.canvas.get_width_height()


@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
def test_animation_delete(anim):
if platform.python_implementation() == 'PyPy':
Expand Down
Loading