A Jupyter magic command for creating reproducible matplotlib figures.
Project description
lusca
lusca is a Python library for creating reproducible matplotlib figures using Jupyter magic commands.
Often, you want to use Jupyter for experiments but may not want to rerun the entire notebook to recreate plots. Additionally, saving data and figures is essential for artifact generation and reproducibility.
📊 %%mplfreeze Command
The %%mplfreeze magic command:
- Captures the data used in your plots and saves it in a compressed NPZ file.
- Automatically exports your figures in multiple useful formats.
- Creates a minimal standalone script that reproduces the figure.
- Snapshots Python/package versions and the git commit into
<name>.meta.json. - Statically checks the cell for unsaved free names before writing anything,
then runs the generated replot in a subprocess to confirm the bundle
actually reproduces the figure — if
%%mplfreezesucceeds, the replot is guaranteed to work. - Leverages
lusca's built-in stylesheet.
Once you're satisfied with your plot, add the %%mplfreeze command to the cell.
%%mplfreeze <name> [vars ...] [--outdir DIR]
<name>: Base name for outputs (folder + files).[vars ...]: Variable names to save into the NPZ file.[--outdir DIR]: (Optional) Parent output directory (default:docs/figs).
Example
- Import and load the magic command
import matplotlib.pyplot as plt
import numpy as np
import lusca
%load_ext lusca
- Some data
x_data = np.linspace(-10, 10, 100)
sine = np.sin(x_data)
cosine = np.cos(x_data)
- Plot + save
%%mplfreeze trig_demo x_data sine cosine
with plt.style.context("lusca"):
fig, ax = plt.subplots(1, 1, figsize=(3.5, 2.6), sharey=True)
ax.plot(x_data, sine, label="Sine")
ax.plot(x_data, cosine, label="Cosine")
plt.show()
An example notebook is available in src/demo.ipynb. The generated plots are saved in docs/figs/ with the following structure:
name_stamp/
name.npz # saved variables
name.pdf # exported figure
name.png
name.svg
name.meta.json # python/package versions + git commit
replot_name.py # standalone replot script
Installation
Install lusca directly from GitHub:
pip install -e git+https://github.com/evmckinney9/lusca#egg=lusca
Note
If you are using VS Code, you can set the workspace root as the default directory for saving figures by adding the following setting to your settings.json file. Otherwise, output paths will be relative to the notebook location.
"jupyter.notebookFileRoot": "${workspaceFolder}"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lusca-0.1.1.tar.gz.
File metadata
- Download URL: lusca-0.1.1.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f99feed8ff044c52dbb364348d34b2311f0c97693f21f3648638c6b3d5b95c36
|
|
| MD5 |
54337e0b290eac1d8174b3145cf05b47
|
|
| BLAKE2b-256 |
9d399aec7518c81cb2ea921f9dfdf395f1f17b8d7f70876f00fe9580ba2b29b4
|
File details
Details for the file lusca-0.1.1-py3-none-any.whl.
File metadata
- Download URL: lusca-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bd17efd22c1ea8ce827cd869f99bd1b9cc91f617db6bb3822bfe259c9d3f50a
|
|
| MD5 |
c7a02a23db40ce61e406e0b264ee79c8
|
|
| BLAKE2b-256 |
fce7192e1156c06ef122a58b68a3df04d54095f8ed989b2a909ec77844ab3762
|