Skip to main content

Make gifs in python.

Project description

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:
    ...

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 after each frame.
    • plt.Figure: uses the current content of the figure as the next image in the gif. No other actions are taken on the figure!
    • 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}.

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 e.g. share notebooks that display this object as a standalone file and the gif will still work.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

make_a_gif-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

make_a_gif-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file make_a_gif-0.1.0.tar.gz.

File metadata

  • Download URL: make_a_gif-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.24

File hashes

Hashes for make_a_gif-0.1.0.tar.gz
Algorithm Hash digest
SHA256 da9c16a19cd21c2b20091cd290c5ebe2fce6660cdbc06ca39c36b494e3039121
MD5 cd0e581feff0745f59150e11618924d8
BLAKE2b-256 220a243e9e374ac2ade8ac070575ef011d8e0dc2aaf1957612882ef1534dda5a

See more details on using hashes here.

File details

Details for the file make_a_gif-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: make_a_gif-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.24

File hashes

Hashes for make_a_gif-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e577c1ece0df3116ed7b60f1db5053ffe6b392d4656cd9a4bcd3accfb5a3cf42
MD5 ce586e72bc29f5ff3a81f05c9782db20
BLAKE2b-256 1960af72732d81a16fd1e010f2073347a54fc675ed010a84f303ab332fe267f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page