Skip to main content

figrid

A wrapper for the matplotlib gridspec function. Designed to make it easy to place axes on a pre-defined grid on a figure canvas. For example, maybe you want to lay out axes like this:

Example Layout

try it out in colab

Open In Colab

how it works

The fundamental function to use is place_axes_on_grid. This will generate an evenly spaced 100x100 grid on the desired figure canvas. You can then specify how much of the figure canvas a given axis (or set of axes) will span.

what it's good for

Maybe it's just me, but I've always found matplotlib's gridspec function to be confusing. And simple NxM grids with the subplots function can be too limiting. This makes it easy to place any number of axes at arbitrary locations on a figure. It's handy for making figures for publication.

a sample workflow

  1. Make some functions to generate the various subplots you want to display on a figure. Those functions should take an axis handle as an input.
  2. Define a figure canvas of the desired size.
  3. Define your axes, specifying their locations using figrid.place_axes_on_grid() (a dictionary is a useful data structure for storing your axis handles).
  4. Call your plotting functions with the axes as inputs.
  5. Add some axis labels using figrid.add_labels() that you can refer to from your figure legend.

installation:

pip install figrid

For development installation with testing dependencies:

pip install -e ".[dev]"

syntax

figrid.place_axes_on_grid takes the following inputs:

  • fig - the figure handle on which the axis will be placed
  • xspan - a two-element list or tuple defining the left and right edges of the axis, respectively. Numbers should be floats ranging from 0 to 1 and will be rounded to 2 decimal places.
  • yspan - a two-element list or tuple defining the top and bottom edges of the axis, respectively. Numbers should be floats ranging from 0 to 1 and will be rounded to 2 decimal places.
  • dim - a two-element tuple defining the number of rows/columns of the axis. Default = [1, 1], giving a single axis.
  • hspace = a float defining the horizontal space between subplots (if dim is specified)
  • vspace = a float defining the vertical space between subplots (if dim is specified)

sample use:

some imports:

# import the package as fg
import figrid as fg

# import example figure code
import example_figures

# import maptlotlib
import matplotlib.pyplot as plt

define a function to lay out the axes on a figure

# define function to set up figure and axes
def make_fig_ax():
    fig = plt.figure(figsize=(11,8.5))
    ax = {
        'panel_A': fg.place_axes_on_grid(fig, xspan=[0.05, 0.3], yspan=[0.05, 0.45]),
        'panel_B': fg.place_axes_on_grid(fig, xspan=[0.4, 1], yspan=[0.05, 0.45], dim=[3, 1], hspace=0.4),
        'panel_C': fg.place_axes_on_grid(fig, xspan=[0.05, 0.4], yspan=[0.57, 1]),
        'panel_D': fg.place_axes_on_grid(fig, xspan=[0.5, 1], yspan=[0.57, 1])
    }
    
    return fig, ax

make the figure

# call function to make figure and axes
fig, ax = make_fig_ax()

# call individual plotting functions, with axes as inputs
example_figures.heatmap(ax['panel_A'])
example_figures.sinusoids(ax['panel_B'])
example_figures.violins(ax['panel_C'])
example_figures.scatterplot(ax['panel_D'])

add some labels

labels = [
    {'label_text':'A', 'xpos':0,    'ypos':0.05, 'fontsize':20, 'weight': 'bold', 'ha': 'right', 'va': 'bottom'},
    {'label_text':'B', 'xpos':0.37, 'ypos':0.05, 'fontsize':20, 'weight': 'bold', 'ha': 'right', 'va': 'bottom'},
    {'label_text':'C', 'xpos':0,    'ypos':0.55, 'fontsize':20, 'weight': 'bold', 'ha': 'right', 'va': 'bottom'},
    {'label_text':'D', 'xpos':0.45, 'ypos':0.55, 'fontsize':20, 'weight': 'bold', 'ha': 'right', 'va': 'bottom'},
]
fg.add_labels(fig, labels)

Then we have this:

Example Figure

development and testing

Tests

For development, install with testing dependencies:

pip install -e ".[dev]"

To set up pre-commit hooks for automatic code formatting:

pre-commit install

This will automatically run black (code formatter) and flake8 (linter) on your commits.

To run the tests:

pytest

To run tests with coverage reporting:

pytest --cov=figrid --cov-report=term-missing

To manually format code:

black .

To manually check code style:

flake8 .

Tests and code quality checks are automatically run on push and pull request to the main branch using GitHub Actions.

Metadata

Release files for figrid 0.1.7

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

Source distribution (sdist)

Source distribution for figrid 0.1.7
File Size Uploaded Provenance Actions
figrid-0.1.7.tar.gz 8.2 kB Without provenance Details for figrid-0.1.7.tar.gz

Built distribution (wheel)

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

Total release size: 15.3 kB

Release files / figrid-0.1.7.tar.gz

Download URL figrid-0.1.7.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
aec44525dd0e226f82f06a2b6b79ec04a0610bdcb3f3d3bca407e211208fa122
BLAKE2b-256 checksum
How to use checksums
721622db16b7a23e9160bb6005fcc38d038fa53b02fc45600b8884d67369fae7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / figrid-0.1.7-py3-none-any.whl

Download URL figrid-0.1.7-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db613a90ac0d482902d3e8e069e5ce2ac961e6f7e1e6a13c1a8fe3ae2b86a5e8
BLAKE2b-256 checksum
How to use checksums
0171fe939717bfd85abdaa5c7f6d03d62455af94a338451fb9a176b67061b5a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for figrid 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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