Skip to content
Draft
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
11 changes: 7 additions & 4 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ def setup(self, fig, outfile, dpi=None):
if dpi is None:
dpi = self.fig.dpi
self.dpi = dpi
fig.set_dpi(self.dpi)

@property
def frame_size(self):
"""A tuple ``(width, height)`` in pixels of a movie frame."""
w, h = self.fig.get_size_inches()
return int(w * self.dpi), int(h * self.dpi)
return self.fig.canvas.get_width_height()

def _supports_transparency(self):
"""
Expand Down Expand Up @@ -293,15 +293,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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the calculation in adjusted_figsize?

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 Expand Up @@ -411,6 +413,7 @@ def setup(self, fig, outfile, dpi=None, frame_prefix=None):
if dpi is None:
dpi = self.fig.dpi
self.dpi = dpi
fig.set_dpi(self.dpi)
self._adjust_frame_size()

if frame_prefix is None:
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