Skip to content

Differences in Animation Creation Between FFmpeg and Pillow Writer #718

@munechika-koyo

Description

@munechika-koyo

Summary

When saving the animation using FuncAnimation's save method with writer="ffmpeg", it produces a strange layout, and layout shifts occur around the first frames compared to webp or gif created with writer="pillow".

Reproducer

import matplotlib
import numpy as np
import ultraplot as uplt
from matplotlib.animation import FuncAnimation

matplotlib.use("Agg")
state = np.random.RandomState(51423)

fig, axs = uplt.subplots(
    nrows=1,
    ncols=2,
    width="14cm",
)

mappables = []
for ax in axs:
    data = state.rand(10, 10)
    m = ax.heatmap(data, cmap="dusk")
    ax.colorbar(m, loc="t", tickdir="out", label="Axes Colorbars")
    mappables.append(m)

axs.format(
    abc="(a)",
    abcloc="ul",
    xlabel="xlabel",
    ylabel="ylabel",
    toplabels=("Left Axes", "Right Axes"),
    urtitle="1",
    suptitle="Test Animation",
)


def update(frame):
    global axs, mappables

    for m in mappables:
        data = state.rand(10, 10)
        m.set_array(data)

    axs.format(
        urtitle=f"{frame + 1}",
    )
    return mappables


ani = FuncAnimation(
    fig,
    update,
    frames=10,
    interval=150,
)

ani.save("test_animation.mp4", writer="ffmpeg")
# ani.save("test_animation.webp", writer="pillow")  # <--- change here

This is the weird video in the MP4 format.

test_animation.mp4

This is the WEBP-formatted one that I probably prefer.
Image

Environment

  • ultraplot v2.2.0
  • ffmpeg v8.1.1
  • python v3.14.4
  • matplotlib v3.10.9

Miscellaneous

This is inherited from the discussion at #488

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions