Skip to main content

Formats multipanel figures

Project description

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

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 subplots 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 handy data structure for storing your axis handles).
  4. Call your plotting functions with the axes as inputs.
  5. Add some axis labels that you can refer to from your figure legend.

installation:

pip install figrid

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

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

figrid-0.1.5.tar.gz (6.3 kB view details)

Uploaded Source

File details

Details for the file figrid-0.1.5.tar.gz.

File metadata

  • Download URL: figrid-0.1.5.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

File hashes

Hashes for figrid-0.1.5.tar.gz
Algorithm Hash digest
SHA256 426565500c8bb20af21a3e06856541b2c370bc7f28bfac656a4f5204f122484c
MD5 75e3725e2ee8baae44afc721798dd2a7
BLAKE2b-256 3e11662b46457b8ca6d6bf52218ab0cfe42a2e507a4a289f85cb7260db69729d

See more details on using hashes here.

Supported by

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