Skip to content

Locations and coordinates

Scilayout places things on a figure in centimetres from its upper left corner, the way you would measure a layout on a page, instead of in matplotlib's figure fractions.

A 12 by 7 cm figure with a centimetre grid, showing the x axis pointing right and the y axis pointing down from the upper left corner, and a panel at (1.5, 1.5) that is 4.5 cm wide and 2.5 cm high

Code for this figure
"""The cm coordinate system, with the guide grid: used on the coordinates page."""

from typing import Any

import matplotlib.patches as patches

import scilayout
from scilayout.locations import CMTransform

FORMATS = ("svg",)

ORANGE = "#d95f02"


def make() -> scilayout.SciFigure:
    """Build the figure."""
    fig = scilayout.figure()
    fig.set_size_cm(12, 7)

    # a panel 4.5 cm wide and 2.5 cm high, with its upper left corner at (1.5, 1.5)
    ax = fig.add_panel((1.5, 1.5, 4.5, 2.5), panellabel="a", method="bounds")
    ax.set_xticks([])
    ax.set_yticks([])
    fig.draw_grid(major_interval=5, minor_interval=1)

    cm = CMTransform(fig)

    def arrow(start: tuple[float, float], end: tuple[float, float]) -> None:
        fig.add_artist(
            patches.FancyArrowPatch(
                *cm.transform(
                    [start, end]
                ),  # cm from the upper left -> figure fraction
                transform=fig.transFigure,
                arrowstyle="-|>",
                mutation_scale=8,
                color=ORANGE,
                linewidth=1.2,
            )
        )

    def label(xy: tuple[float, float], text: str, **kwargs: Any) -> None:
        fig.text(
            *cm.transform([xy])[0],
            text,
            transform=fig.transFigure,
            color=ORANGE,
            fontsize=8,
            **kwargs,
        )

    # the axes, starting at the origin in the upper left
    arrow((0.3, 0.3), (3.2, 0.3))
    label((3.3, 0.3), "x (cm)", va="center")
    arrow((0.3, 0.3), (0.3, 3.2))
    label((0.1, 3.3), "y (cm)", ha="left", va="top")
    label((0.45, 0.45), "(0, 0)", ha="left", va="top")
    # the size of the panel
    arrow((1.5, 4.6), (6.0, 4.6))
    label((3.75, 4.8), "width 4.5", ha="center", va="top")
    arrow((6.7, 1.5), (6.7, 4.0))
    label((6.9, 2.75), "height 2.5", ha="left", va="center")
    label((1.6, 1.4), "(1.5, 1.5)", ha="left", va="bottom")
    return fig
  • The origin is the upper left of the figure.
  • y increases downwards. This is the opposite of matplotlib's data and figure coordinates, where y increases upwards.
  • Everything that takes a position in cm uses this convention: panels, panel labels, figure text and cm scalebar positions.

Describing a rectangle

A rectangle, such as a panel, can be described in two ways. Scilayout calls them bounds and extent, which are the same words matplotlib uses for Bbox.bounds and Bbox.extents.

Values Meaning Name
bounds (x, y, w, h) upper left corner, then width and height Bound
extent (x0, y0, x1, y1) upper left corner, then lower right corner Extent

These two panels are the same:

import scilayout

fig = scilayout.figure()
fig.set_size_cm(12, 8)

fig.add_panel((1.5, 1.5, 4.5, 2.5), method="bounds")  # x, y, width, height
fig.add_panel((1.5, 1.5, 6.0, 4.0), method="extent")  # x0, y0, x1, y1

Use bounds when you know how big a panel should be, for example a journal column width. Use extents when you want to line edges up with something else.

The method argument

SciFigure.add_panel, PanelAxes and PanelAxes.set_location take a location tuple and a method that says how to read it:

method Reads the tuple as
"bounds" (x, y, width, height)
"extent" (x0, y0, x1, y1)

Bounds with a negative width or height, and extents whose corners are the wrong way round, raise a ValueError.

The default is changing

If you don't give a method, a location is currently read as an extent, and scilayout emits a FutureWarning. In version 1.0 the default will change to bounds. Pass method="extent" to keep your current behaviour, or method="bounds" to adopt the new one. Either removes the warning.

The older names "bbox" (now "extent") and "size" (now "bounds") still work but are deprecated, and will be removed in version 1.0.

Reading a location back

PanelAxes.get_location always returns an Extent, whichever way the panel was created. It is a named tuple, so it unpacks like one and its fields can be read by name:

ax = fig.add_panel((1.5, 1.5, 4.5, 2.5), method="bounds")

x0, y0, x1, y1 = ax.get_location()
ax.get_location().x1  # 6.0

Converting between units

scilayout.locations has the conversions that scilayout uses itself. For example, cm_to_fraction and fraction_to_cm move between cm from the upper left and matplotlib's figure fraction. CMTransform is a matplotlib transform from cm to figure fractions.