Skip to main content

✨ The extension for matplotlib and Altair animations

Project description

gif

GitHub Travis PyPI Downloads

About

The extension for matplotlib and Altair animations.

Installation

gif is installed at the command line:

pip install gif

Altair gifs require additional dependencies. These can be installed accordingly:

pip install gif[altair]

Note: altair-saver uses Selenium, which requires a properly configured installation of either chromedriver or geckodriver.

Usage (matplotlib)

Imports and data:

import random
from matplotlib import pyplot as plt
import gif

x = [random.randint(0, 100) for _ in range(100)]
y = [random.randint(0, 100) for _ in range(100)]

Decorate a plot function with gif.frame (and don't return anything):

@gif.frame
def plot(i):
    xi = x[i*10:(i+1)*10]
    yi = y[i*10:(i+1)*10]
    plt.scatter(xi, yi)
    plt.xlim((0, 100))
    plt.ylim((0, 100))

Build a bunch of "frames" with a standard for loop:

frames = []
for i in range(10):
    frame = plot(i)
    frames.append(frame)

Specify the duration (in milliseconds) between each frame, and save:

gif.save(frames, 'example.gif', duration=100)

Usage (Altair)

Imports and data:

import random
import altair as alt
import pandas as pd
import gif

df = pd.DataFrame({
    't': list(range(10)) * 10,
    'x': [random.randint(0, 100) for _ in range(100)],
    'y': [random.randint(0, 100) for _ in range(100)]
})

Decorate a plot function with gif.frame and return an Altair object:

@gif.frame
def plot(i):
    d = df[df['t'] == i]
    chart = alt.Chart(d).encode(
        x=alt.X('x', scale=alt.Scale(domain=(0, 100))),
        y=alt.Y('y', scale=alt.Scale(domain=(0, 100)))
    ).mark_circle()
    return chart

Build a bunch of "frames" with a standard for loop:

frames = []
for i in range(10):
    frame = plot(i)
    frames.append(frame)

Specify the duration (in milliseconds) between each frame, and save:

gif.save(frames, 'example.gif', duration=100)

Gallery (matplotlib)

Click on any image to see the source code

attachment.gif hop.gif phone.gif
seinfeld.gif attachment.gif

Gallery (Altair)

Click on any image to see the source code

covid.gif emoji.gif pyramid.gif
textbooks.gif wave.gif

If you have a kick ass animation that you think should be in the Gallery, submit a PR!

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

gif-2.0.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Supported by

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