Skip to content

Commit 94c71aa

Browse files
author
Dylan
committed
Adding 2d macros
1 parent d622ebd commit 94c71aa

File tree

14 files changed

+471
-10
lines changed

14 files changed

+471
-10
lines changed

LICENSE.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2021 Dylan Walsh
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of the plotly-gif Developers nor the names of any
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Plotly-gif
2+
3+
---
4+
---
5+
6+
A simple python package to generate .gif from your plotly figures. It works for both 2D and 3D figures. It can even
7+
create_gif motion for you in 3D plots.
8+
9+
## Installation
10+
11+
```
12+
pip install plotly-gif
13+
```
14+
15+
### Dependencies
16+
17+
If you are already using plotly, then you should be good. But, just in case, these are the dependencies:
18+
```python
19+
pip install numpy
20+
pip install plotly
21+
pip install kaleido # used by plotly to generate png
22+
pip install Pillow # used to convert png to gif
23+
```
24+
25+
---
26+
## Usage
27+
28+
There are three common methods:
29+
30+
### Built-in Functions
31+
32+
33+
34+
### Decorator
35+
36+
If you have a function that is changing the `go.Figure' with each loop, you can add the decorator to the func.
37+
38+
```python
39+
import plotly.graph_objs as go
40+
from plotly_gif import GIF, capture
41+
42+
gif = GIF()
43+
44+
@capture(gif)
45+
def plot_(x_, y_):
46+
fig = go.Figure()
47+
# add your traces()
48+
# add your formatting()
49+
50+
return fig
51+
52+
gif.create_gif() # generate gif
53+
```
54+
55+
### In-Line
56+
This very similar to the decorator option, but you can call the image capture function directly.
57+
58+
```python
59+
import plotly.graph_objs as go
60+
from plotly_gif import GIF, capture
61+
62+
gif = GIF()
63+
64+
def plot_(x_, y_):
65+
fig = go.Figure()
66+
# add your traces()
67+
# add your formatting()
68+
69+
gif.create_image(fig) # create_gif image for gif
70+
71+
return fig
72+
73+
gif.create_gif() # generate gif
74+
```
75+
76+
---
77+
## Options
78+
79+
80+
---1
81+
## Examples
82+

examples/example_1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import plotly.graph_objs as go
3+
from plotly_gif import GIF, capture
4+
5+
6+
# create_gif some data
7+
n = 100
8+
x = np.linspace(0, n-1, n)
9+
y = np.sin(x) + np.random.rand(n)
10+
11+
# Create gif class to store data
12+
gif = GIF(verbose=True)
13+
14+
15+
@capture(gif) # tells gif to save each figure that is generate from this function
16+
def plot_(x_, y_):
17+
# function that generates plotly figures
18+
fig = go.Figure()
19+
fig.add_trace(go.Scatter(x=x_, y=y_, mode="lines"))
20+
21+
return fig
22+
23+
24+
frames = 60
25+
for i in range(1, n, int(n/frames)):
26+
# Changes the range of the data each step to make it look like a time series
27+
plot_(x[0: i], y[0: i])
28+
29+
# Create gif
30+
gif.create_gif()

examples/example_1_option2.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import plotly.graph_objs as go
3+
from plotly_gif import GIF
4+
5+
6+
# create_gif some data
7+
n = 100
8+
x = np.linspace(0, n-1, n)
9+
y = np.sin(x) + np.random.rand(n)
10+
11+
# Create gif class to store data
12+
gif = GIF(verbose=True)
13+
14+
15+
def plot_(x_, y_):
16+
# Function that generates plotly figures
17+
fig = go.Figure()
18+
fig.add_trace(go.Scatter(x=x_, y=y_, mode="lines"))
19+
20+
gif.create_image(fig) # tells gif to save each figure
21+
return fig
22+
23+
24+
frames = 60
25+
for i in range(1, n, int(n/frames)):
26+
# Changes the range of the data each step to make it look like a time series
27+
plot_(x[0: i], y[0: i])
28+
29+
# Function that generates new plotly figures
30+
gif.create_gif()

examples/example_1_short.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
import plotly.graph_objs as go
3+
from plotly_gif import GIF, two_d_time_series
4+
5+
6+
# create_gif some data
7+
n = 100
8+
x = np.linspace(0, n-1, n)
9+
y = np.sin(x) + np.random.rand(n)
10+
11+
# create and format plot just the way you like!
12+
fig = go.Figure()
13+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines"))
14+
fig.update_layout(layout)
15+
fig.update_xaxes(xaxis)
16+
fig.update_yaxes(yaxis)
17+
18+
# create gif class to store data
19+
gif = GIF(verbose=True)
20+
21+
# send it to built-in function that will generate gif time_series.
22+
two_d_time_series(gif, fig)

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=42",
4+
"wheel"
5+
]
6+
build-backend = "setuptools.build_meta"

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Pillow==9.0.0
2+
plotly==5.5.0
3+
kaleido==0.2.1

setup.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ package_dir =
2424
=src
2525
zip_safe = no
2626
include_package_data = true
27-
27+
install_requires =
28+
Pillow>=9.0.0
29+
plotly>=5.5.0
30+
kaleido>=0.2.1
2831

src/plotly_gif/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from functools import wraps
2+
import logging
3+
4+
from .gif import GIF
5+
from .two_d_plots import two_d_time_series
6+
from .three_d_plots import three_d_scatter_rotate
7+
8+
level = logging.INFO
9+
logging.basicConfig(level=level, format=' %(message)s')
10+
11+
12+
def capture(gif_: GIF):
13+
""" Capture
14+
Captures images as they are created.
15+
16+
Parameters
17+
----------
18+
gif_: GIF
19+
gif object that stores images
20+
21+
"""
22+
def capture_decorator(func):
23+
@wraps(func)
24+
def _capture(*args, **kwargs):
25+
fig = func(*args, **kwargs)
26+
gif_.create_image(fig)
27+
return fig
28+
29+
return _capture
30+
31+
return capture_decorator

src/plotly_gif/_format.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Just trying to make the plots look nice!! :)
2+
layout = {
3+
"autosize": False,
4+
"width": 800,
5+
"height": 600,
6+
"showlegend": False,
7+
"font": dict(family="Arial", size=18, color="black"),
8+
"plot_bgcolor": "white"
9+
}
10+
11+
xaxis = {
12+
"title": "<b>X<b>",
13+
"tickprefix": "<b>",
14+
"ticksuffix": "</b>",
15+
"showline": True,
16+
"linewidth": 5,
17+
"mirror": True,
18+
"linecolor": 'black',
19+
"ticks": "outside",
20+
"tickwidth": 4,
21+
"showgrid": False,
22+
"gridwidth": 1,
23+
"gridcolor": 'lightgray'
24+
}
25+
26+
yaxis = {
27+
"title": "<b>Y<b>",
28+
"tickprefix": "<b>",
29+
"ticksuffix": "</b>",
30+
"showline": True,
31+
"linewidth": 5,
32+
"mirror": True,
33+
"linecolor": 'black',
34+
"ticks": "outside",
35+
"tickwidth": 4,
36+
"showgrid": False,
37+
"gridwidth": 1,
38+
"gridcolor": 'lightgray'
39+
}
40+
41+
fig.update_layout(layout)
42+
fig.update_xaxes(xaxis)
43+
fig.update_yaxes(yaxis)

0 commit comments

Comments
 (0)