Skip to content

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

Description

@hirohira9119

Bug summary

After upgrading matplotlib from 3.11.0 to 3.11.1, GIF frames generated with animation.PillowWriter become slightly skewed to the left.
This distortion does not occur in 3.11.0.

Code for reproduction

from matplotlib import animation
from matplotlib import pyplot as plt

fig, ax = plt.subplots(figsize=(8.2, 7), dpi=100)
line, = ax.plot([], [])

def update(frame):
    line.set_data([0, 1], [0, frame])
    return line,

ani = animation.FuncAnimation(fig, update, frames=1, blit=False)

ani.save("test.gif", writer=animation.PillowWriter(fps=1), dpi=100)

plt.close(fig)

Actual outcome

matplotlib 3.11.1
Image

Expected outcome

matplotlib 3.11.0
Image

Additional information

To investigate the cause, I used a custom DebugPillowWriter to inspect the canvas size and frame size during saving.

from io import BytesIO

import matplotlib

class DebugPillowWriter(animation.PillowWriter):
    def grab_frame(self, **savefig_kwargs):
        buffer = BytesIO()

        self.fig.savefig(
            buffer,
            format="rgba",
            dpi=self.dpi,
            **savefig_kwargs,
        )

        width, height = self.frame_size

        raw_size = buffer.getbuffer().nbytes
        expected_size = width * height * 4

        print("Matplotlib:", matplotlib.__version__)
        print("canvas size:", self.fig.canvas.get_width_height())
        print("frame size:", self.frame_size)
        print("raw size:", raw_size)
        print("expected size:", expected_size)

        return super().grab_frame(**savefig_kwargs)

In 3.11.1, the canvas size and frame size differ by 1 pixel:

Matplotlib: 3.11.1
canvas size: (820, 700)
frame size: (819, 700)
raw size: 2296000
expected size: 2293200

In 3.11.0, they match:

Matplotlib: 3.11.0
canvas size: (819, 700)
frame size: (819, 700)
raw size: 2293200
expected size: 2293200

This mismatch in 3.11.1 seems related to the skewing observed in the GIF output.

Operating system

Windows

Matplotlib Version

3.11.1

Matplotlib Backend

No response

Python version

3.14

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions