Skip to content

Commit

Permalink
11 update
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhumber committed Nov 16, 2022
1 parent 2fb8b64 commit 1a93d88
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 171 deletions.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test:
pytest

format:
isort .
black .

types:
mypy gif.py
pyright gif.py

loc:
find . -name 'gif.py' | xargs wc -l | sort -nr
find tests -name '*.py' | xargs wc -l | sort -nr
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,30 @@

The [matplotlib](https://matplotlib.org/) Animation Extension


### Quickstart

Install
### Install & Import

```sh
pip install gif
```

Usage
```python
import gif
```

### Quickstart

```python
import random
from matplotlib import pyplot as plt
import gif
from random import randint
from matplotlib import pyplot as plt

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

# (Optional) Set the dots per inch resolution to 300:
# (Optional) Set the dots per inch resolution to 300
gif.options.matplotlib["dpi"] = 300

# Decorate a plot function with @gif.frame (return not required):
# Decorate a plot function with @gif.frame
@gif.frame
def plot(i):
xi = x[i*10:(i+1)*10]
Expand All @@ -42,22 +43,19 @@ def plot(i):
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)
# Construct "frames"
frames = [plot(i) for i in range(10)]

# Specify the duration between frames (milliseconds) and save to file:
# Save "frames" to gif with a specified duration (milliseconds) between each frame
gif.save(frames, 'example.gif', duration=50)
```


### Examples

| [![arrival.gif](images/arrival.gif)](examples/arrival.py) | [![hop.gif](images/hop.gif)](examples/hop.py) | [![phone.gif](images/phone.gif)](examples/phone.py) |
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| [![seinfeld.gif](images/seinfeld.gif)](examples/seinfeld.py) | [![spiral.gif](images/spiral.gif)](examples/spiral.py) | [![love.gif](images/love.gif)](love.py) |
| [![arrival.gif](images/arrival.gif)](examples/arrival.py) | [![hop.gif](images/hop.gif)](examples/hop.py) | [![phone.gif](images/phone.gif)](examples/phone.py) |
| ------------------------------------------------------------ | ------------------------------------------------------ | --------------------------------------------------- |
| [![seinfeld.gif](images/seinfeld.gif)](examples/seinfeld.py) | [![spiral.gif](images/spiral.gif)](examples/spiral.py) | [![heart.gif](images/heart.gif)](heart.py) |


### Warning
Expand Down
7 changes: 4 additions & 3 deletions examples/arrival.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import gif

import random
from collections import Counter

from matplotlib import pyplot as plt
from PIL import Image

import gif

random.seed(2020)


Expand Down Expand Up @@ -38,4 +39,4 @@ def simulate_arrival(count, p=0.10):
frames.append(frame)
count_last = count.copy()

gif.save(frames, "images/matplotlib-arrival.gif")
gif.save(frames, "images/arrival.gif")
59 changes: 0 additions & 59 deletions examples/beating_heart.py

This file was deleted.

58 changes: 58 additions & 0 deletions examples/heart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import matplotlib.pyplot as plt
import numpy as np

import gif

COLOR = "#d66582"
SIZE = 3


def random_scatter(x, y, beta=0.15, seed=None):
np.random.seed(seed)
ratio_x = -beta * np.log(np.random.rand(x.shape[0]))
ratio_y = -beta * np.log(np.random.rand(y.shape[0]))
dx = ratio_x * x
dy = ratio_y * y
return x - dx, y - dy


def plot_random_scatter(ax, x, y, beta, c=COLOR, s=SIZE, alpha=None, seed=None):
x, y = random_scatter(x, y, beta=beta, seed=seed)
ax.scatter(x, y, s=3, c=c, alpha=alpha)


@gif.frame
def plot_heart(x, y, i):
fig = plt.figure(figsize=(5, 3), dpi=100, facecolor="white")
ax = plt.gca()
ax.set_facecolor("white")
x = x * np.sin(i)
y = y * np.sin(i)
ax.scatter(x, y, s=SIZE, c=COLOR)
plot_random_scatter(ax, x, y, 0.15, seed=1)
plot_random_scatter(ax, x, y, 0.15, seed=2)
plot_random_scatter(ax, x, y, 0.15, seed=3)
xi = x[: x.shape[0] : 2] * np.sin(i) * 0.7
yi = y[: y.shape[0] : 2] * np.sin(i) * 0.7
plot_random_scatter(ax, xi, yi, 0.25, seed=4)
xo = x[: x.shape[0] : 2] * np.sin(i) * 1.2
yo = y[: y.shape[0] : 2] * np.sin(i) * 1.2
plot_random_scatter(ax, xo, yo, 0.1, alpha=0.8, seed=6)

for spine in ax.spines.values():
spine.set_visible(False)
ax.tick_params(bottom=False, labelbottom=False, left=False, labelleft=False)
plt.xlim((-6 * np.pi, 6 * np.pi))
plt.ylim((-6 * np.pi, 6 * np.pi))
plt.tight_layout(pad=0)


frames = []
for i in np.linspace(np.pi / 3, 2 * np.pi / 3, 20):
t = np.linspace(0, 2 * np.pi, 2000)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
frame = plot_heart(x, y, i)
frames.append(frame)

gif.save(frames, "images/heart.gif", duration=100)
6 changes: 3 additions & 3 deletions examples/hop.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import gif

import matplotlib.pyplot as plt
import numpy as np

import gif

N = 50
red = np.random.normal(loc=45, scale=3, size=N)
blue = np.random.normal(loc=48, scale=5, size=N)
Expand All @@ -24,4 +24,4 @@ def plot_hop(i, margin=0.1):
frame = plot_hop(i)
frames.append(frame)

gif.save(frames, "images/matplotlib-hop.gif", duration=200)
gif.save(frames, "images/hop.gif", duration=200)
25 changes: 0 additions & 25 deletions examples/love.py

This file was deleted.

7 changes: 3 additions & 4 deletions examples/phone.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import gif

from matplotlib import pyplot as plt
import pandas as pd
from matplotlib import pyplot as plt

import gif

START = pd.Timestamp("2019-04-20")
END = pd.Timestamp("2020-05-01")
Expand Down Expand Up @@ -35,4 +34,4 @@ def plot(date):
frame = plot(date)
frames.append(frame)

gif.save(frames, "images/matplotlib-phone.gif", duration=35)
gif.save(frames, "images/phone.gif", duration=35)
8 changes: 4 additions & 4 deletions examples/seinfeld.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import gif

import re
from matplotlib import pyplot as plt

import pandas as pd
from matplotlib import pyplot as plt

import gif

# script cleaning
df = pd.read_csv("gallery/data/seinfeld.csv")
Expand Down Expand Up @@ -82,4 +82,4 @@ def plot(episode):
frame = plot(episode)
frames.append(frame)

gif.save(frames, "images/matplotlib-seinfeld.gif", duration=100)
gif.save(frames, "images/seinfeld.gif", duration=100)
8 changes: 4 additions & 4 deletions examples/spiral.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import gif

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

import gif

N = 100

Expand Down Expand Up @@ -30,4 +30,4 @@ def plot_spiral(i):
frame = plot_spiral(i)
frames.append(frame)

gif.save(frames, "images/matplotlib-spiral.gif", duration=50)
gif.save(frames, "images/spiral.gif", duration=50)
Loading

0 comments on commit 1a93d88

Please sign in to comment.