# celluloid
[![Build Status](https://travis-ci.com/jwkvam/celluloid.svg?branch=master)](https://travis-ci.com/jwkvam/celluloid)
[![codecov](https://codecov.io/gh/jwkvam/celluloid/branch/master/graph/badge.svg)](https://codecov.io/gh/jwkvam/celluloid)
[![pypi](https://badge.fury.io/py/celluloid.svg)](https://pypi.org/project/celluloid/)
[![pypi versions](https://img.shields.io/pypi/pyversions/celluloid.svg)](https://pypi.org/project/celluloid/)
Easy Matplotlib Animation
<p align="center">
<a href="https://github.com/jwkvam/celluloid/blob/master/examples/sines.py">
<img src="https://user-images.githubusercontent.com/86304/48657442-9c11e080-e9e5-11e8-9f54-f46a960be7dd.gif">
</a>
</p>
Creating animations should be easy.
This module makes it easy to adapt your existing visualization code to create an animation.
## Install
```
pip install celluloid
```
## Manual
Follow these steps:
1. Create a matplotlib `Figure` and create a `Camera` from it:
```python
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
```
2. Reusing the figure and after each frame is created, take a snapshot with the camera.
```python
plt.plot(...)
plt.fancy_stuff()
camera.snap()
```
3. After all frames have been captured, create the animation.
```python
animation = camera.animate()
animation.save('animation.mp4')
```
The entire [module](https://github.com/jwkvam/celluloid/blob/master/celluloid.py) is less than 50 lines of code.
## Examples
### Minimal
As simple as it gets.
```python
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for i in range(10):
plt.plot([i] * 10)
camera.snap()
animation = camera.animate()
```
<p align="center">
<a href="https://github.com/jwkvam/celluloid/blob/master/examples/simple.py">
<img src="https://user-images.githubusercontent.com/86304/48666133-66660980-ea70-11e8-9024-b167c21a5e83.gif">
</a>
</p>
### Subplots
Animation at the top.
```python
import numpy as np
from matplotlib import pyplot as plt
from celluloid import Camera
fig, axes = plt.subplots(2)
camera = Camera(fig)
t = np.linspace(0, 2 * np.pi, 128, endpoint=False)
for i in t:
axes[0].plot(t, np.sin(t + i), color='blue')
axes[1].plot(t, np.sin(t - i), color='blue')
camera.snap()
animation = camera.animate()
```
### Images
Domain coloring example.
```python
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import hsv_to_rgb
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for a in np.linspace(0, 2 * np.pi, 30, endpoint=False):
x = np.linspace(-3, 3, 800)
X, Y = np.meshgrid(x, x)
x = X + 1j * Y
y = (x ** 2 - 2.5) * (x - 2.5 * 1j) * (x + 2.5 * 1j) \
* (x - 2 - 1j) ** 2 / ((x - np.exp(1j * a)) ** 2
* (x - np.exp(1j * 2 * a)) ** 2)
H = np.angle(y) / (2 * np.pi) + .5
r = np.log2(1. + np.abs(y))
S = (1. + np.abs(np.sin(2. * np.pi * r))) / 2.
V = (1. + np.abs(np.cos(2. * np.pi * r))) / 2.
rgb = hsv_to_rgb(np.dstack((H, S, V)))
ax.imshow(rgb)
camera.snap()
animation = camera.animate()
```
<p align="center">
<a href="https://github.com/jwkvam/celluloid/blob/master/examples/complex.py">
<img src="https://user-images.githubusercontent.com/86304/48747098-f483f080-ec26-11e8-9734-c409e9b0c9ec.gif">
</a>
</p>
### Legends
```python
import matplotlib
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for i in range(5):
t = plt.plot(range(i, i + 5))
plt.legend(t, [f'line {i}'])
camera.snap()
animation = camera.animate()
```
<p align="center">
<a href="https://github.com/jwkvam/celluloid/blob/master/examples/complex.py">
<img src="https://user-images.githubusercontent.com/86304/48750564-9100bf80-ec34-11e8-87fb-bc5c7ddcc6e7.gif">
</a>
</p>
## Limitations
- The axes' limits should be the same for all plots. The limits of the animation will be the limits of the final plot.
- Legends will accumulate from previous frames. Pass the artists to the `legend` function to draw them separately.
## Credits
Inspired by [plotnine](https://github.com/has2k1/plotnine/blob/master/plotnine/animation.py).
Raw data
{
"_id": null,
"home_page": "https://github.com/jwkvam/celluloid",
"name": "celluloid",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "matplotlib animation",
"author": "Jacques Kvam",
"author_email": "jwkvam+pypi@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/0d/31/80e96c4b221de342eef55f1f07de84b11b5f7cfb8c3b00e235a0bdd0b476/celluloid-0.2.0.tar.gz",
"platform": "",
"description": "# celluloid\n\n[![Build Status](https://travis-ci.com/jwkvam/celluloid.svg?branch=master)](https://travis-ci.com/jwkvam/celluloid)\n[![codecov](https://codecov.io/gh/jwkvam/celluloid/branch/master/graph/badge.svg)](https://codecov.io/gh/jwkvam/celluloid)\n[![pypi](https://badge.fury.io/py/celluloid.svg)](https://pypi.org/project/celluloid/)\n[![pypi versions](https://img.shields.io/pypi/pyversions/celluloid.svg)](https://pypi.org/project/celluloid/)\n\nEasy Matplotlib Animation\n\n<p align=\"center\">\n <a href=\"https://github.com/jwkvam/celluloid/blob/master/examples/sines.py\">\n <img src=\"https://user-images.githubusercontent.com/86304/48657442-9c11e080-e9e5-11e8-9f54-f46a960be7dd.gif\">\n </a>\n</p>\n\nCreating animations should be easy.\nThis module makes it easy to adapt your existing visualization code to create an animation.\n\n## Install\n\n```\npip install celluloid\n```\n\n## Manual\n\nFollow these steps:\n\n1. Create a matplotlib `Figure` and create a `Camera` from it:\n\n```python\nfrom celluloid import Camera\nfig = plt.figure()\ncamera = Camera(fig)\n```\n\n2. Reusing the figure and after each frame is created, take a snapshot with the camera.\n\n```python\nplt.plot(...)\nplt.fancy_stuff()\ncamera.snap()\n```\n\n3. After all frames have been captured, create the animation.\n\n```python\nanimation = camera.animate()\nanimation.save('animation.mp4')\n```\n\nThe entire [module](https://github.com/jwkvam/celluloid/blob/master/celluloid.py) is less than 50 lines of code.\n\n## Examples\n\n### Minimal\n\nAs simple as it gets.\n\n```python\nfrom matplotlib import pyplot as plt\nfrom celluloid import Camera\n\nfig = plt.figure()\ncamera = Camera(fig)\nfor i in range(10):\n plt.plot([i] * 10)\n camera.snap()\nanimation = camera.animate()\n```\n\n<p align=\"center\">\n <a href=\"https://github.com/jwkvam/celluloid/blob/master/examples/simple.py\">\n <img src=\"https://user-images.githubusercontent.com/86304/48666133-66660980-ea70-11e8-9024-b167c21a5e83.gif\">\n </a>\n</p>\n\n### Subplots\n\nAnimation at the top.\n\n```python\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom celluloid import Camera\n\nfig, axes = plt.subplots(2)\ncamera = Camera(fig)\nt = np.linspace(0, 2 * np.pi, 128, endpoint=False)\nfor i in t:\n axes[0].plot(t, np.sin(t + i), color='blue')\n axes[1].plot(t, np.sin(t - i), color='blue')\n camera.snap()\nanimation = camera.animate()\n```\n\n### Images\n\nDomain coloring example.\n\n```python\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom matplotlib.colors import hsv_to_rgb\n\nfrom celluloid import Camera\n\nfig = plt.figure()\ncamera = Camera(fig)\n\nfor a in np.linspace(0, 2 * np.pi, 30, endpoint=False):\n x = np.linspace(-3, 3, 800)\n X, Y = np.meshgrid(x, x)\n x = X + 1j * Y\n y = (x ** 2 - 2.5) * (x - 2.5 * 1j) * (x + 2.5 * 1j) \\\n * (x - 2 - 1j) ** 2 / ((x - np.exp(1j * a)) ** 2\n * (x - np.exp(1j * 2 * a)) ** 2)\n\n H = np.angle(y) / (2 * np.pi) + .5\n r = np.log2(1. + np.abs(y))\n S = (1. + np.abs(np.sin(2. * np.pi * r))) / 2.\n V = (1. + np.abs(np.cos(2. * np.pi * r))) / 2.\n\n rgb = hsv_to_rgb(np.dstack((H, S, V)))\n ax.imshow(rgb)\n camera.snap()\nanimation = camera.animate()\n```\n\n<p align=\"center\">\n <a href=\"https://github.com/jwkvam/celluloid/blob/master/examples/complex.py\">\n <img src=\"https://user-images.githubusercontent.com/86304/48747098-f483f080-ec26-11e8-9734-c409e9b0c9ec.gif\">\n </a>\n</p>\n\n### Legends\n\n```python\nimport matplotlib\nfrom matplotlib import pyplot as plt\nfrom celluloid import Camera\n\nfig = plt.figure()\ncamera = Camera(fig)\nfor i in range(5):\n t = plt.plot(range(i, i + 5))\n plt.legend(t, [f'line {i}'])\n camera.snap()\nanimation = camera.animate()\n```\n\n<p align=\"center\">\n <a href=\"https://github.com/jwkvam/celluloid/blob/master/examples/complex.py\">\n <img src=\"https://user-images.githubusercontent.com/86304/48750564-9100bf80-ec34-11e8-87fb-bc5c7ddcc6e7.gif\">\n </a>\n</p>\n\n## Limitations\n\n- The axes' limits should be the same for all plots. The limits of the animation will be the limits of the final plot.\n- Legends will accumulate from previous frames. Pass the artists to the `legend` function to draw them separately.\n\n## Credits\n\nInspired by [plotnine](https://github.com/has2k1/plotnine/blob/master/plotnine/animation.py).\n",
"bugtrack_url": null,
"license": "",
"summary": "Easy matplotlib animation.",
"version": "0.2.0",
"split_keywords": [
"matplotlib",
"animation"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "e960f4accf0881b6f8cbfaf993669dba",
"sha256": "5cebd41276d9658f7667a2f1f565f566763a6110273b4af35c958b1407a9f277"
},
"downloads": -1,
"filename": "celluloid-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e960f4accf0881b6f8cbfaf993669dba",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 5404,
"upload_time": "2018-11-20T04:06:22",
"upload_time_iso_8601": "2018-11-20T04:06:22.374042Z",
"url": "https://files.pythonhosted.org/packages/60/a7/7fbe80721c6f1b7370c4e50c77abe31b4d5cfeb58873d4d32f48ae5a0bae/celluloid-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "78da3ef8ff3944f253b16a923e427380",
"sha256": "568b1512c4a97483759e9436c3f3e5dc5566da350179aa1872992ec8d82706e1"
},
"downloads": -1,
"filename": "celluloid-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "78da3ef8ff3944f253b16a923e427380",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 6442,
"upload_time": "2018-11-20T04:06:27",
"upload_time_iso_8601": "2018-11-20T04:06:27.228809Z",
"url": "https://files.pythonhosted.org/packages/0d/31/80e96c4b221de342eef55f1f07de84b11b5f7cfb8c3b00e235a0bdd0b476/celluloid-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2018-11-20 04:06:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "jwkvam",
"github_project": "celluloid",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "celluloid"
}