Skip to main content

The extension for matplotlib, Altair, and Plotly animations

Project description

gif

GitHub Travis PyPI Downloads

About

The extension for Altair, matplotlib, and Plotly animations.

Installation

gif is installed at the command line:

pip install -U gif

Depending on which flavour of gif you plan to use you'll likely need some additional dependencies:

pip install "gif[altair]"     
pip install "gif[matplotlib]"
pip install "gif[plotly]"

Note: gif[altair] uses Selenium, which requires a properly configured chromedriver or geckodriver.

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 between each frame and save:

gif.save(frames, 'example.gif', duration=100, unit="ms", between="frames")

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)]

(Optional) Set the dots per inch resolution to 300:

gif.options.matplotlib["dpi"] = 300

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 of the entire gif:

gif.save(frames, 'example.gif', duration=3.5, unit="s", between="startend")

Usage (Plotly)

Imports and data:

import random
import plotly.graph_objects as go
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 a Plotly figure:

@gif.frame
def plot(i):
    d = df[df['t'] == i]
    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=d["x"],
        y=d["y"],
        mode="markers"
    ))
    fig.update_layout(width=500, height=300)
    return fig

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 (milliseconds) between each frame and save:

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

Gallery (Altair)

Click on any image to see the source code

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

Gallery (matplotlib)

Click on any image to see the source code

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

Gallery (Plotly)

Click on any image to see the source code

bubble.gif swirl.gif waterfall.gif

If you have a dope 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-3.0.0.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file gif-3.0.0.tar.gz.

File metadata

  • Download URL: gif-3.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

File hashes

Hashes for gif-3.0.0.tar.gz
Algorithm Hash digest
SHA256 432f36e1830440be4a4b1b84fc7ae834bf3c7180edb07ed3bf5919bfb107d22a
MD5 aae3da80a470d8597a68676b6281dc5d
BLAKE2b-256 5841a556a028c7c04dba76ced9bbfb321ef63df48d50153d18291b433a3d9a89

See more details on using hashes here.

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