Skip to content

Locations

locations

Convert between cm, inch, and fraction locations in matplotlib figures.

LocationMethod module-attribute

LocationMethod = Literal['bounds', 'extent']

How to read a 4-tuple location.

  • "bounds": (x, y, width, height)
  • "extent": (x0, y0, x1, y1), the corners

CMTransform

CMTransform(fig: Figure)

Bases: Transform

Convert coordinates from centimeters to figure fractions.

Works on a Matplotlib figure, with the origin at the upper left.

Parameters:

Name Type Description Default
fig Figure

The figure to calculate the transform on.

required

transform

transform(values: ArrayLike) -> ndarray

Transform cm coordinates (upper left origin) to figure fractions.

inverted

inverted() -> InvertedCMTransform

Return the inverse transform.

InvertedCMTransform

InvertedCMTransform(fig: Figure, *args: Any, **kwargs: Any)

Bases: Transform

Convert figure fractions to centimeters (upper left origin).

transform

transform(values: ArrayLike) -> ndarray

Transform figure fractions to cm coordinates (upper left origin).

inverted

inverted() -> CMTransform

Return the inverse transform.

locationcm_to_position

locationcm_to_position(
    fig: Figure,
    location_corners: tuple[
        centimetres, centimetres, centimetres, centimetres
    ],
) -> tuple[float, float, float, float]

Convert an upper left origin cm extent to a figure fraction rect.

Parameters:

Name Type Description Default
fig Figure

Figure to find the true size from.

required
location_corners tuple of float

(x0, y0, x1, y1) corners in cm, origin at the upper left of the figure with y increasing downwards.

required

Returns:

Type Description
tuple of float

(left, bottom, width, height) in figure fraction, origin at the lower left of the figure with y increasing upwards. This is the form matplotlib's add_axes and set_position take.

Raises:

Type Description
ValueError

If x0 > x1 or y0 > y1.

cm_to_inch

cm_to_inch(cm: float) -> float
cm_to_inch(cm: ndarray) -> ndarray
cm_to_inch(cm: float | ndarray) -> float | ndarray

Convert centimeters to inches.

inch_to_cm

inch_to_cm(inch: float) -> float
inch_to_cm(inch: ndarray) -> ndarray
inch_to_cm(inch: float | ndarray) -> float | ndarray

Convert inches to centimeters.

cm_to_fraction

cm_to_fraction(
    fig: Figure, xy: tuple[float, float]
) -> tuple[float, float]

Convert a position in cm from the upper left to figure fraction.

Parameters:

Name Type Description Default
fig Figure

Figure to find the size from.

required
xy tuple of float

(x, y) in cm, origin at the upper left of the figure.

required

Returns:

Type Description
tuple of float

(x, y) in figure fraction, origin at the lower left of the figure.

fraction_to_cm

fraction_to_cm(
    fig: Figure, xy: tuple[float, float]
) -> tuple[centimetres, centimetres]

Convert a figure fraction position to cm from the upper left.

Parameters:

Name Type Description Default
fig Figure

Figure to find the size from.

required
xy tuple of float

(x, y) in figure fraction, origin at the lower left of the figure.

required

Returns:

Type Description
tuple of float

(x, y) in cm, origin at the upper left of the figure.