Skip to main content

make-a-gif

A (very) simple package for generating gifs using matplotlib.

Installation

pip install make-a-gif

or just copy across the make_a_gif.py file to your project.

Usage

For a full suite of examples, see the examples directory - note that make-a-gif works well with Jupyter notebooks.

import matplotlib.pyplot as plt
from make_a_gif import gif


# define a function that generates a plot for a given frame
def plotting_func(i: int) -> None:
    xs = range(i + 1)
    ys = [x**2 for x in xs]
    plt.plot(xs, ys, "-o", clip_on=False, zorder=10)
    # make sure the limits are always the same
    plt.xlim(0, 10)
    plt.ylim(0, 100)


# create the gif
gif(plotting_func, frames=range(11), fps=2, save_to="squared.gif")

Documentation

The only object exported by make-a-gif is the gif function.

gif(
    frames: Iterable[Frame],
    function: Callable[[Frame], None | str | Path | Figure],
    save_to: str | Path | None = None,
    fps: float = 10,
    css: dict[str, str] | None = None,
    savefig_kwargs: dict[str, Any] | None = None,
) -> HTML | None:
    ...

Description:

Generate a GIF from a sequence of frames.

Based on the return type of the function, the following happens:

  • None: assume that the currently active matplotlib figure has been contains the desired image. Use this figure for the next frame, and then close the figure.
  • str or Path: assume that this points to an image file. This gets used as the next image in the gif.
  • plt.Figure: use the current content of the matplotlib figure as the next image in the gif. The figure is not closed.

The function is called for each frame, and the return values are used to generate images for the gif in order. In pseudocode:

images = []

for frame in frames:
    ret = function(frame)
    if isinstance(ret, str | Path):
        images.append(Image.open(ret))
    elif isinstance(ret, Figure):
        images.append(ret.savefig())
    elif ret is None:
        images.append(plt.savefig())

return gif(images)

Parameters:

  • frames is an iterable of arbitrary objects. These are passed in order, and one-by-one to the function.
  • function takes an arbitrary frame object as input, and generates a single image for the gif. There are several behaviours here, depending on the return type of function:
    • None: assume that a matplotlib plot has been generated. This is then used as the next image in the gif. The figure is closed automatically after each frame.
    • plt.Figure: uses the current content of the figure as the next image in the gif. The figure is not closed.
    • str or Path: assume that this points to an image file. This gets used as the next image in the gif.
  • save_to is the path to save the gif to. If not provided, the gif is not saved.
  • fps is the frames per second of the gif, by default 10
  • css is the CSS to apply to the HTML returned by the function.
  • savefig_kwargs are the keyword arguments to pass to plt.savefig when saving the figure to a file. The default is {"bbox_inches": "tight", "transparent": True}.
  • loop is the loop mode of the gif:
    • "infinite": loop the gif indefinitely
    • "once": play the gif once
    • "bounce": play the gif forwards and then backwards indefinitely

Returns:

gif returns an IPython.display.HTML object. This contains a base64 encoded version of the gif, and so is independent of the file system - you can e.g. share notebooks that display this object as a standalone file and the gif will still work.

Metadata

Release files for make-a-gif 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for make-a-gif 0.2.0
File Size Uploaded Provenance Actions
make_a_gif-0.2.0.tar.gz 5.5 kB Without provenance Details for make_a_gif-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for make-a-gif 0.2.0
File Interpreter ABI Platform Actions
make_a_gif-0.2.0-py3-none-any.whl Python 3 none any Details for make_a_gif-0.2.0-py3-none-any.whl

Total release size: 12.1 kB

Release files / make_a_gif-0.2.0.tar.gz

Download URL make_a_gif-0.2.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
deefd23ee979ea5b6d95e45a92caf84ae9bf7358cc3e02dd345852062ba2c541
BLAKE2b-256 checksum
How to use checksums
2545e74f26dd0bde5b25a4e7cf9cf44bf354842a431bc3d1d898bd0d4d4bbeda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.24

Release files / make_a_gif-0.2.0-py3-none-any.whl

Download URL make_a_gif-0.2.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23348fd729933e9ba86b46aab9ec6abbd0abe150f4ab5ca3de71a9ad89b34723
BLAKE2b-256 checksum
How to use checksums
5f350d3ef734756fdd021a3875c6626d16d94f427546ccd2c26e5675befc8e53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.24

Security for make-a-gif 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page