Skip to main content

Smartplot

This package provides a simple contextmanager which automatically saves data used to make matplotlib plots and automatically generates a script to reproduce the plot at a later stage.

Install

pip install smartplot

Basic Usage

A SmartPlotContext contextmanager can be used in conjunction with a with block as shown below.

from smartplot import SmartPlotContext


with SmartPlotContext("./data-save-dir", locals(), overwrite=True) as spl:
    spl.plot( ... )
    ...
    spl.title( ... )
    spl.show()

The resulting context manager spl support all matplotlib.pyplot function calls. All calls made to spl are passed onto matplotlib.pyplot as-is and recorded. The data save directory is dynamically created if it does not exist and is populated with pickle files containing the data used to make the plot. The automatically generated make_plot.py script contains code which reloads all data and contains the necessary calls to matplotlib.pyplot used to create the original plot. This script may then be modified to adjust the plot. If the data save directory is not empty an exception is raised unless overwrite=True. Passing in locals() allows SmartPlotContext to infer the original variable names for the objects used for re-use in make_plot.py.

Note that only calls directly to spl are recorded, if you use figures and axes directly, you're on your own for now.

Example Usage

import numpy as np
import matplotlib.pyplot as plt
from smartplot import SmartPlotContext


x_data = np.linspace(0, 2 * np.pi, 1000)
y_data = np.sin(x_data)
with SmartPlotContext("./data-save-dir", locals(), overwrite=True) as spl:
    spl.plot(x_data, y_data, c='k', label='sin')
    spl.plot(x_data, np.cos(x_data), c='b', label='cos')
    spl.xlabel('xaxis')
    spl.ylabel('yaxis')
    spl.title('Title')
    spl.legend()
    spl.savefig('plot.pdf')
    spl.show()

This will generate the following directory structure

data-save-dir/
  make_plot.py
  unnamed_arg.pkl
  x_data.pkl
  y_data.pkl

The contents of make_plot.py would be

import numpy as np
import matplotlib.pyplot as plt
from smartplot import load_pickle


x_data = load_pickle('x_data.pkl')
y_data = load_pickle('y_data.pkl')
unnamed_arg = load_pickle('unnamed_arg.pkl')

plt.plot(x_data, y_data, c='k', label='sin')
plt.plot(x_data, unnamed_arg, c='b', label='cos')
plt.xlabel('xaxis')
plt.ylabel('yaxis')
plt.title('Title')
plt.legend()
plt.savefig('plot.pdf')
plt.show()

which should perfectly reproduce the original plot. Note that the variable names x_data and y_data are arbitrary and were inferred by the SmartPlotContext using the locals() dict. Good code structure will always allow SmartPlotContext to infer the correct variable names. Anonymous arguments, such as the y data of the np.cos plot are simply saved and named "unnamed_arg" with an integer postfix if multiple unnamed arguments are present.

Misc

Please report any bugs or feature requests to jero.wilkinson@gmail.com.

Metadata

Release files for smartplot 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smartplot 0.1.2
File Size Uploaded
smartplot-0.1.2.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smartplot 0.1.2
File Interpreter ABI Platform
smartplot-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / smartplot-0.1.2.tar.gz

Download URL smartplot-0.1.2.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f23a6742babd496becf8283b5ebf4d9bd1218372857765f35fefff2e31934b73
BLAKE2b-256 checksum
How to use checksums
01d4856395b3cbddd1f7229d46691129ad1cb4aa9b0fa6a1c225d5bc2fd3f1c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release files / smartplot-0.1.2-py3-none-any.whl

Download URL smartplot-0.1.2-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ed21bd1c709fd3fc325802b681e2cc95df9e026d725b10de4944819be9c47b3
BLAKE2b-256 checksum
How to use checksums
7a26c88f9c38886f6571ed4813da9a1907116635b27eed626fcea2c62236a49c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page