Skip to content

Scale bars

scalebars

Movable scalebars for matplotlib plots.

ScaleBar

ScaleBar(
    ax: Axes,
    xy: tuple[float, float],
    length: float,
    unit: str | None = None,
    coordSystem: str = "fraction",
    orientation: str = "h",
    hpos: str | None = None,
    vpos: str | None = None,
    transform: str = "transData",
    textstring: str | None = None,
    lw: float | None = None,
    fontsize: float | None = None,
    pad: float | None = None,
)

Scale bar for matplotlib plots.

Horizontal or vertical, with text that can sit beside the bar or in a break in it. A scalebar can be repositioned using the move method.

Attributes:

Name Type Description
line Line2D

The bar.

text Text

The label.

pad float

Gap in cm between the edge of the line and the text.

Parameters:

Name Type Description Default
ax Axes

Axes to draw the scalebar on.

required
xy tuple of float

Position of the start of the scalebar (its left end if horizontal, its lower end if vertical), in units of coordSystem.

required
length float

Length of the scalebar in data units.

required
unit str

Unit of the scalebar, shown after the length in the default text.

None
coordSystem (fraction, data, cm)

Units of xy: "fraction" of the axes (default), "data" coordinates, or "cm" from the upper left of the figure (needs a PanelAxes).

"fraction"
orientation (h, horizontal, v, vertical)

Orientation of the scalebar, horizontal by default.

"h"
hpos (left, center, right)

Where the text sits horizontally. Only used for a vertical bar, where it is "left" by default. "center" puts the text in the middle of the bar, with a gap in the bar around the text.

"left"
vpos (top, center, bottom)

Where the text sits vertically. Only used for a horizontal bar, where it is "bottom" by default. "center" puts the text in the middle of the bar, with a gap in the bar around the text.

"top"
transform (transData, transAxes)

Transform of the scalebar. Only "transData" is implemented.

"transData"
textstring str

Text to display. Defaults to the length and unit.

None
lw float

Linewidth of the scalebar. Defaults to the "scalebars.linewidth" style parameter.

None
fontsize float

Font size of the text. Defaults to the "scalebars.fontsize" style parameter.

None
pad float

Gap in cm between the edge of the line and the text. It applies beside the bar and, for text centred on the bar, on each side of the text in the break in the bar. Defaults to the "scalebars.pad" style parameter. It can be changed later, followed by move.

None

Raises:

Type Description
ValueError

If orientation, hpos, vpos, coordSystem or transform is not one of the allowed values, or pad is negative.

NotImplementedError

If transform is "transAxes".

TypeError

If coordSystem is "cm" and ax is not a PanelAxes.

Warns:

Type Description
UserWarning

If the text is centred on the bar but is longer than the bar, in which case the bar is not drawn.

move

move(
    x: float, y: float, coordSystem: str | None = None
) -> None

Move the scalebar to a new position.

Parameters:

Name Type Description Default
x float

x-coordinate of the new position.

required
y float

y-coordinate of the new position.

required
coordSystem (fraction, data, cm)

Units of x and y. Defaults to the units the scalebar was created with.

"fraction"

Raises:

Type Description
ValueError

If coordSystem is not recognised.

LinkedScaleBar

LinkedScaleBar(
    ax: Axes,
    xy: tuple[float, float],
    x_length: float,
    y_length: float,
    x_unit: str | None = None,
    y_unit: str | None = None,
    coordSystem: str = "fraction",
    hat: str | None = None,
    vat: str | None = None,
    transform: str = "transData",
    x_textstring: str | None = None,
    y_textstring: str | None = None,
    lw: float | None = None,
    pad: float | None = None,
)

A horizontal and a vertical scale bar that are moved together.

Parameters:

Name Type Description Default
ax Axes

Axes to draw the scalebars on.

required
xy tuple of float

Position of the shared corner, in units of coordSystem.

required
x_length float

Length of the horizontal bar in data units.

required
y_length float

Length of the vertical bar in data units.

required
x_unit str

Unit of the horizontal bar.

None
y_unit str

Unit of the vertical bar.

None
coordSystem (fraction, data, cm)

Units of xy.

"fraction"
hat (left, center, right)

Horizontal position of the vertical bar's text.

"left"
vat (top, center, bottom)

Vertical position of the horizontal bar's text.

"top"
transform (transData, transAxes)

Transform of the scalebars. Only "transData" is implemented.

"transData"
x_textstring str

Text of the horizontal bar. Defaults to its length and unit.

None
y_textstring str

Text of the vertical bar. Defaults to its length and unit.

None
lw float

Linewidth of both bars.

None
pad float

Gap in cm between the edge of each line and its text. Defaults to the "scalebars.pad" style parameter.

None

move

move(
    x: float, y: float, coordSystem: str | None = None
) -> None

Move both scale bars to a new position.

Parameters:

Name Type Description Default
x float

x-coordinate of the shared corner.

required
y float

y-coordinate of the shared corner.

required
coordSystem (fraction, data, cm)

Units of x and y. Defaults to the units the bars were created with.

"fraction"