Skip to main content

PltPack

Bind your matplotlib functions with data

Test

PyPI version fury.io PyPI pyversions PyPI license PyPI status

PltPack is a lightweight packaging tool for storing and exchanging data & code bound in a single file. It is mainly created to accelerate scientific work and is focused on supporting matplotlib package for exchanging scientific figures. Integrated with Jupyter Notebook.

Install from PyPi with pip:

pip install plt_pack

Usage

PltPack saves minimal atomic part of your code and the data required to run a function and plot a figure:

  • arguments
  • imports
  • function code
  • called sub-functions
  • global variables
  • used module versions
  • non-default matplotlib rcParams

The most convenient way to use PltPack is to define a project with a folder where all the .plt files will be saved, and register the entry functions you would like to save. The alternative is to decorate a function with 'auto_save' method to save it with new arguments on every call.

Below is a self-explanatory example:

# imports
from matplotlib import pyplot as plt
import numpy as np

from plt_pack import PltProject

# define your project with directory for figures
plt_project = PltProject('FiguresDir')

# define your functions
# decorate them with 'register' or 'auto_save' method

@plt_project.register
def plot(x, y_list, label_list):
    for y, label in zip(y_list, label_list):
        plot_line(x, y, label)
    add_legend()


@plt_project.auto_save(rewrite=False, datefmt='%H-%M-%S')
def plot_hist(y, bins: int = 10):
    plt.hist(y, bins=bins)


# some util functions & globals defined somewhere in your
# file or Jupyter Notebook

COLOR = 'red'

def plot_line(x, y, label: str = None, ls: str = '--'):
    plt.plot(x, y, ls=ls, lw=2, c=COLOR, label=label)

def add_legend():
    plt.legend()
    
    
# Registered function can be saved with context parameters:
x = np.arange(10)
y_list = [np.arange(10) * i for i in range(5)]
label_list = [f'Curve #{i}' for i in range(5)]

with plt_project(rewrite=True, name='my_function'):
    plot(x, y_list, label_list)
    
# function will be executed but also saved to my_function.plt
# file to your project folder:

assert plt_project.list_files() == ['my_function']

Now you can upload this file later (non necessarily on the same machine) and reproduce the result with one call.

from plt_pack import read_plt_file

file = read_plt_file('my_function')
# or using plt_project: 
# file = plt_project.load_file('path/to/file')

file.exec()  # that will re-run the function

print(file.get_code_str())  # that will show all the code to reproduce it

Expected output:

# Imports:
import matplotlib.pyplot as plt

# Global variables:
COLOR = 'red'

# Main function:

def plot(x, y_list, label_list):
    for y, label in zip(y_list, label_list):
        plot_line(x, y, label)
    add_legend()


# Sub-functions:

def add_legend():
    plt.legend()


def plot_line(x, y, label: str = None, ls: str = '--'):
    plt.plot(x, y, ls=ls, lw=2, c=COLOR, label=label)

LICENSE

MIT

Metadata

Release files for plt-pack 0.0.3.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 plt-pack 0.0.3.2
File Size Uploaded
plt_pack-0.0.3.2.tar.gz 16.6 kB Details

Release files / plt_pack-0.0.3.2.tar.gz

Download URL plt_pack-0.0.3.2.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1bd8ee66c3bc6f8af98c38335fbb4b23ee963a5ef10ad4e153c11870fd732fef
BLAKE2b-256 checksum
How to use checksums
a12ec76d5cc28426619a5669d5de0c90f96363282b5066e2e3b08b80310875ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.0.3.2 This release

1 release file

0.0.3.1

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1.4

1 release file

0.0.1.3

1 release file

0.0.1.2

1 release file

0.0.1.1

1 release file

0.0.1

1 release file

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