Skip to content

Fix the rounding of animation frame size to match the eventual canvas size - #32194

Open
ayshih wants to merge 1 commit into
matplotlib:mainfrom
ayshih:animation_canvas_size
Open

Fix the rounding of animation frame size to match the eventual canvas size#32194
ayshih wants to merge 1 commit into
matplotlib:mainfrom
ayshih:animation_canvas_size

Conversation

@ayshih

@ayshih ayshih commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

PR summary

As reported in #32186, the change in #32038 to have appropriately rounded canvas sizes made the necessary updates to the backends, but missed that matplotlib.animation also needed to be updated. This PR updates matplotlib.animation to calculate the frame size with the same rounding as will be used for the canvas size during saving.

Closes #32186

AI Disclosure

No AI was used

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New features and API changes have release notes
  • [N/A] Documentation complies with general and docstring guidelines

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the calculation in adjusted_figsize() is "fine" to stay as-is given how it uses np.nextafter()

@ayshih
ayshih force-pushed the animation_canvas_size branch 2 times, most recently from 5ec6ef1 to 8d07fc0 Compare August 14, 2026 20:21
@ayshih ayshih changed the title Make the frame size of animation writers be the canvas size rather than the figure size Fix the rounding of animation frame size to match the eventual canvas size Aug 17, 2026
@ayshih
ayshih force-pushed the animation_canvas_size branch from 8d07fc0 to dcc9091 Compare August 17, 2026 13:28
@ayshih

ayshih commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

With apologies to @fudianchn, the first code change in this PR ended up being the same as in the now-closed #32189. Despite what I wrote in #32189 (comment), it turns out that we cannot simply query the canvas size of the existing figure because the animation may end up saving the figure using a different DPI – either implicitly or explicitly set – than the existing figure's DPI. There are contortions that could be considered, but I think at that point it's better to simply repeat the same rounding tolerance.

@ayshih
ayshih marked this pull request as ready for review August 17, 2026 17:25
@QuLogic QuLogic added this to the v3.11.2 milestone Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: GIF frames become skewed when using PillowWriter in matplotlib 3.11.1

2 participants