Skip to main content

Constraint-based matplotlib figure layout

Project description

mplayout

Constraint-based figure layout for matplotlib.

Define rows and columns with fixed, fractional, or aspect-ratio-driven sizes. The layout is solved as a linear system before any axes are created, so every panel lands exactly where you specify — no post-hoc tight_layout surprises.

Installation

pip install -e .          # editable install from this directory

Dependencies: matplotlib, numpy.

Size specs

Spec Meaning
1.5 / '1.5in' / '15mm' / '1cm' Fixed size in inches (bare numbers are inches)
'1fr' / '2fr' Proportional share of the remaining space (requires fig_height when used in rows)
'auto' Determined by the aspect-ratio constraint of the panel(s) in that track

Quick start

from mplayout import Grid

g = Grid(
    rows=['auto', '1fr'],
    cols=['auto', '5mm', 'auto'],
    hgap='2mm', wgap='2mm', margin='5mm',
)

p_img_left  = g.panel(row=0, col=0, aspect=1.0)
p_img_right = g.panel(row=0, col=2, aspect=4/3)
p_data      = g.panel(row=1, col=0, colspan=3)

fig, axes = g.build(fig_width='3.5in', fig_height='3in')

axes[p_img_left].set_title('Image A')
axes[p_img_right].set_title('Image B')
axes[p_data].set_title('Data')

build() returns the figure and a dict mapping each Panel handle to its matplotlib.axes.Axes. The fig_height is optional for layouts that contain no 'fr' rows — in that case the height is inferred from the aspect constraints.

API

Grid(rows, cols, *, hgap=0, wgap=0, gap=None, margin=0)

Create a layout grid.

  • rows, cols — lists of size specs, one per track.
  • hgap, wgap — gap between rows / columns. A single spec or a list of nrows−1 / ncols−1 values for per-gap control. Must be fixed sizes.
  • gap — shorthand that sets both hgap and wgap to the same value.
  • margin — whitespace around the content area. One value (all sides), two values (vertical, horizontal), or four values (top, right, bottom, left).

Grid.panel(row, col, *, rowspan=1, colspan=1, aspect=None) → Panel

Register a panel. Returns an opaque Panel handle used as the key in the axes dict returned by build().

  • aspect — height-to-width ratio. Required for 'auto' tracks; ignored for fully fixed/fr tracks.

Grid.fill(aspect=None) → list[list[Panel]]

Create one panel per cell and return them as panels[row][col].

Grid.subgrid(row, col, *, rowspan=1, colspan=1, rows, cols, hgap=0, wgap=0, gap=None) → Grid

Nest a child grid inside the given cell(s). Returns the child Grid, which supports the same panel(), fill(), and subgrid() calls.

Grid.build(fig_width, fig_height=None) → (Figure, dict[Panel, Axes])

Solve the layout and create the figure. fig_width is required and must be a fixed size. fig_height is inferred when omitted (not allowed for 'fr' rows).

Examples

File What it shows
examples/basic.py Two square panels side by side with a gap and margin
examples/mixed_sizing.py Fixed header row, fractional body row, auto-sized image columns
examples/subgrid.py 2×2 image grid nested inside one cell of a larger layout

Run any example from the project root:

python3 examples/basic.py

Each script saves a PNG next to itself.


Built with Claude Code.

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

mplayout-0.1.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mplayout-0.1.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file mplayout-0.1.0.tar.gz.

File metadata

  • Download URL: mplayout-0.1.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for mplayout-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2ed89b0655d66878c863c52e9a98feea073c3a3f006fdedab5c91e49b29f8a38
MD5 b71c262c31701fe884ed9c596ed9edb2
BLAKE2b-256 8d5ca0b98fc99a58560574f5b2f71aae16a9d0e98a9a19db6383341ceeb6623a

See more details on using hashes here.

File details

Details for the file mplayout-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mplayout-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for mplayout-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4b88b628391d185b5eb5baa5a389efbe131969c06749aaeb4e7ab215dd9c926
MD5 f6a8749758dd86fc050a279c24e3ec23
BLAKE2b-256 375bcd15a1dfb724913a7fbd66a4d06e5b62dbe3e0b8d5a33a3e74d2487dafef

See more details on using hashes here.

Supported by

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