Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

gif

GitHub PyPI Downloads

About

The animation extension for matplotlib, Altair, and Plotly graphs

matplotlib

Install

pip install "gif[matplotlib]"

Quickstart

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

Examples

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

Altair

Install

pip install "gif[altair]"

Note: requires Selenium and a properly configured chromedriver or geckodriver

Quickstart

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 (return not required):
@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"
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")

Examples

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

Plotly

Install

pip install "gif[plotly]"

Quickstart

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

Examples

bubble.gif swirl.gif waterfall.gif