Skip to main content

savefigs

CI workflow status Version on PyPI

Easily save all open Matplotlib figures, with useful filenames.

Usage

Assume we have a script some_script.py that creates multiple Matplotlib figures.

Import the savefigs function:

from savefigs import savefigs

The below examples assume the figures do not have labels (fig.get_label()). If a figure does have a label, it will be used in place of fig{num}.

Default save settings (./{script filename stem}{figure label or fig{num}}.png):

savefigs()
# ./some_script_fig1.png, ./some_script_fig2.png, ...

👆 The filenames tell us which script generated the figures as well as their relative places in the figure generation order (or labels if they are labeled).

Specify directory:

savefigs(save_dir="figs")  # must exist
# ./figs/some_script_fig1.png, ./figs/some_script_fig2.png, ...

Specify a different prefix to the base stem format:

savefigs(stem_prefix="run1")
# ./run1_fig1.png, ./run1_fig2.png, ...

Save in multiple file formats:

savefigs(formats=["png", "pdf"])
# ./some_script_fig1.png, ./some_script_fig1.pdf, ...

Avoid overwriting files:

savefigs(clobber=False, noclobber_method="add_num")
# ./some_script_fig3.png (assuming ./some_script_fig{1,2}.png already exist)

👆 By default (without changing noclobber_method), setting clobber=False will instead error.

Background

When writing a script that creates multiple figures, I usually label them (usually using the num argument to plt.figure()/plt.subplots()), which makes it easier to find the correct figure window. Then, at the end of the script I write a loop like:

for num in plt.get_fignums():
    fig = plt.figure(num)
    fig.savefig(f"{fig.get_label()}.pdf", ...)
    # Maybe another format...

savefigs() essentially does this, but is more robust and provides additional features through keyword arguments. And it saves having to write those lines in the script, instead allowing the simple one-liner:

from savefigs import savefigs; savefigs()

Metadata

Release files for savefigs 0.2.1

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

Source distribution (sdist)

Source distribution for savefigs 0.2.1
File Size Uploaded
savefigs-0.2.1.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for savefigs 0.2.1
File Interpreter ABI Platform
savefigs-0.2.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 14.3 kB

Release files / savefigs-0.2.1.tar.gz

Download URL savefigs-0.2.1.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7b8935ef0895e9f2f49f1e8f3527d017222cf22373c7d025feb18c0a33ac1a89
BLAKE2b-256 checksum
How to use checksums
ca7581504e769b53e151fe4422f45ab328c21f2777f5fadb1eddd3846771cfb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release files / savefigs-0.2.1-py2.py3-none-any.whl

Download URL savefigs-0.2.1-py2.py3-none-any.whl
Size 6.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2ead50439a2d0e3089be0494f8f886ffa9b2cc9de86a6e4c65feda4b5796ca68
BLAKE2b-256 checksum
How to use checksums
f6713c80ae0324a0295cb6861b53ddf5eec37386b9baba6aee6a47921250abb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.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