Reference
snaplot.main.Camera
A class for capturing matplotlib figures and exporting them as a GIF.
It works with every visualization library based on matplotlib such as seaborn, plotnine, etc, and matplotlib itself.
Under the hood, it saves all intermediate files on the disk and
combine them at the end using the gifing
package.
Attributes:
| Name | Type | Description |
|---|---|---|
verbose |
bool
|
If True, prints log messages during execution. |
directory |
str
|
Directory path where images are stored. |
n_images |
int
|
Count of currently saved images. |
file_paths |
list
|
List of saved image file paths. |
get_files()
Retrieve all current intermediate file paths.
snap(fig=None, extension='png', **kwargs)
Take a snapshot of your latest plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
The figure to save. If None, uses the current active figure. |
None
|
extension
|
str
|
File extension/format to save the figure (e.g., 'png', 'jpg'). |
'png'
|
**kwargs
|
Dict
|
Additional keyword arguments passed to |
{}
|
start(record_id, *, force_new=False, verbose=True)
classmethod
Initiate a Camera instance and start to record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record_id
|
int
|
Id used to make sure |
required |
force_new
|
bool
|
If True, re-start the recording from 0 (and 'forget'
all previous images). This is useful to avoid accidently delete
intermediate images when running |
False
|
verbose
|
bool
|
If True, enables logging of actions. |
True
|
stop(path, frame_duration=100, n_repeat_last_frame=1, resolution='auto')
Compile the saved images into a GIF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Output path for the final GIF. |
required |
frame_duration
|
int
|
Duration of each frame in milliseconds. |
100
|
n_repeat_last_frame
|
int
|
Number of times to repeat the last frame. |
1
|
resolution
|
Union[str, Tuple, List]
|
An optional array with 2 integers (width and height, in pixels) for the resolution of the GIF. By default, it will use the dimensions of the last image in inches and convert them to pixels. |
'auto'
|
Usage
import matplotlib.pyplot as plt
from snaplot import Camera
camera = Camera.start()
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 2, 3]) # first chart
camera.snap()
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 2, 3], color="red") # second chart
camera.snap()
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 5, 3], color="green", lw=3) # third chart
camera.snap()
fig, ax = plt.subplots()
ax.plot([5, 2, 4], [2, 3, 3], color="blue", lw=6) # fourth chart
camera.snap()
camera.stop("my_file.gif", frame_duration=300)
