Skip to main content

A Jupyter magic command for creating reproducible matplotlib figures.

Project description

lusca

CI Python Ruff

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 %%mplfreeze succeeds, 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

  1. Import and load the magic command
import matplotlib.pyplot as plt
import numpy as np
import lusca
%load_ext lusca
  1. Some data
x_data = np.linspace(-10, 10, 100)
sine = np.sin(x_data)
cosine = np.cos(x_data)
  1. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lusca-0.1.1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lusca-0.1.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

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

Hashes for lusca-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f99feed8ff044c52dbb364348d34b2311f0c97693f21f3648638c6b3d5b95c36
MD5 54337e0b290eac1d8174b3145cf05b47
BLAKE2b-256 9d399aec7518c81cb2ea921f9dfdf395f1f17b8d7f70876f00fe9580ba2b29b4

See more details on using hashes here.

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

Hashes for lusca-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5bd17efd22c1ea8ce827cd869f99bd1b9cc91f617db6bb3822bfe259c9d3f50a
MD5 c7a02a23db40ce61e406e0b264ee79c8
BLAKE2b-256 fce7192e1156c06ef122a58b68a3df04d54095f8ed989b2a909ec77844ab3762

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page