Classes
classes
Classes for handling panel layout in cm with upper left origin.
LocationCM
module-attribute
LocationCM = tuple[
centimetres, centimetres, centimetres, centimetres
]
Location in cm (x0, y0, x1, y1) or (x, y, w, h), origin upper left.
SciFigure
Bases: Figure
Figure that handles panel layout in cm with an upper left origin.
A matplotlib.figure.Figure with methods to set its size in cm
(set_size_cm), add panels positioned in cm (add_panel), show a cm
guide grid (draw_grid) and save with settings made for documents
(export). Create one with scilayout.figure.
Attributes:
| Name | Type | Description |
|---|---|---|
grid |
GuideGridClass
|
Handler for the guide grid. Internal, use |
transCM |
CMTransform
|
Transform from cm (upper left origin) to figure fraction. |
Examples:
>>> import scilayout
>>> fig = scilayout.figure()
>>> fig.set_size_cm(12, 8)
>>> ax = fig.add_panel((1.5, 1.5, 4.5, 2.5), method="bounds", panellabel="a")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments for |
()
|
**kwargs
|
Any
|
Keyword arguments for |
{}
|
set_size_cm
Set size of figure in cm.
Panels keep their location in cm from the upper left of the figure, and the
guide grid is redrawn to fit. This applies when the size is changed with this
method: setting the size with matplotlib's own set_size_inches leaves
panels at the same fraction of the figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w
|
float
|
Width (cm) of figure. |
required |
h
|
float
|
Height (cm) of figure. |
required |
add_panel
add_panel(
location: LocationCM,
panellabel: str | None = None,
method: str | None = None,
**kwargs: Any,
) -> PanelAxes
Add a panel (axes) to the figure, positioned in cm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
tuple of float
|
Location of the panel in cm from the upper left of the figure, read
according to |
required |
panellabel
|
str
|
Letter to label the panel with, for example "a". |
None
|
method
|
('bounds', 'extent')
|
How to interpret the location. "bounds" reads (x, y, width, height). "extent" reads (x0, y0, x1, y1), the upper left and lower right corners. Not specifying a method currently means "extent", but the default will change to "bounds" in version 1.0. Specify it to avoid the warning. The old names "bbox" and "size" are deprecated aliases of "extent" and "bounds", and will be removed in version 1.0. |
"bounds"
|
**kwargs
|
Any
|
Keyword arguments for |
{}
|
Returns:
| Type | Description |
|---|---|
PanelAxes
|
The new panel. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
draw_grid
draw_grid(
major_interval: float | None = None,
minor_interval: float | None = None,
half_spacer: bool | None = None,
) -> None
Show the cm guide grid on the figure.
The grid has markers measured from the upper left of the figure: larger ones
every major_interval cm (5 by default), smaller ones every
minor_interval cm (1), and optionally crosses halfway between those. It is
removed by clear. Settings given here
are kept for the next time the grid is drawn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
major_interval
|
float
|
Spacing of the large markers in cm. |
None
|
minor_interval
|
float
|
Spacing of the small markers in cm. |
None
|
half_spacer
|
bool
|
Whether to add crosses halfway between the small markers. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an interval is not positive. |
clear
clear(keep_observers: bool = False) -> None
Clear the figure, including the panels and the guide grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keep_observers
|
bool
|
Passed to |
False
|
clf
clf(keep_observers: bool = False) -> None
set_location
Set location of a figure window on the screen.
Only the Qt backends are supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
int
|
Horizontal coordinate from the left of the screen. |
required |
y
|
int
|
Vertical coordinate from the top of the screen. |
required |
method
|
'px'
|
Units of |
"px"
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If |
RuntimeError
|
If the figure has no window. |
export
Export the figure to a file, with settings made for documents.
The format comes from the file extension: ".png", ".svg", ".pdf" or ".eps". SVG text stays as text, PDF and EPS metadata that changes on every save is removed (so version control sees no change), and PNGs are transparent by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
savepath
|
str or Path
|
The path to save the figure to. |
required |
**kwargs
|
Any
|
Options for the export: |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file extension is not recognised. |
FileExistsError
|
If the file exists and |
PanelAxes
PanelAxes(
fig: Figure,
location: LocationCM,
panellabel: str | None = None,
method: str | None = None,
**kwargs: Any,
)
Bases: Axes
Axes that are positioned in cm from the upper left of the figure.
A matplotlib.axes.Axes where the position is called a location, is in cm
with the origin at the upper left of the figure, and is set and read with
set_location and get_location. Create one with
SciFigure.add_panel.
Attributes:
| Name | Type | Description |
|---|---|---|
panellabel |
PanelLabel or None
|
The panel's label, or None if it has none. See |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Figure to add the panel to. |
required |
location
|
tuple of float
|
Location of the panel in cm from the upper left of the figure, read
according to |
required |
panellabel
|
str
|
Letter to label the panel with, for example "a". |
None
|
method
|
('bounds', 'extent')
|
How to interpret |
"bounds"
|
**kwargs
|
Any
|
Keyword arguments for |
{}
|
set_location
set_location(
location: LocationCM, method: str | None = None
) -> None
Set the location of the panel in cm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
tuple of float
|
Coordinates from the upper left of the figure in cm, read according to
|
required |
method
|
('bounds', 'extent')
|
"bounds" reads (x, y, width, height). "extent" reads (x0, y0, x1, y1), the upper left and lower right corners. Not specifying a method currently means "extent", but the default will change to "bounds" in version 1.0. Specify it to avoid the warning. |
"bounds"
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
get_location
get_location() -> ExtentCM
Get the location of the panel in cm.
Returns:
| Type | Description |
|---|---|
Extent
|
(x0, y0, x1, y1) in cm, origin at the upper left of the figure. It unpacks like a tuple and its fields are also available by name. |
add_label
add_label(
label: str,
ha: Literal["left", "center", "right"] | None = None,
va: Literal[
"baseline",
"bottom",
"center",
"center_baseline",
"top",
]
| None = None,
) -> None
Add a label to the panel, or change its text if it already has one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
Identifier string for the panel, for example "a". |
required |
ha
|
('left', 'center', 'right')
|
Horizontal alignment of the text. See
|
"left"
|
va
|
('baseline', 'bottom', 'center', 'center_baseline', 'top')
|
Vertical alignment of the text. |
"baseline"
|
PanelLabel
A label for a multi-part figure.
The label's baseline starts at exactly the upper left corner of the axes and is then moved by the 'panellabel.xoffset' and 'panellabel.yoffset' style parameters (cm, negative is left and up). The defaults put the letter just above and to the left of the corner, clear of a 10 point y tick label at the top of the axis. Use a smaller (less negative) offset for smaller tick labels.
The 'anchor position' is the top left corner of the associated PanelAxes.
To change a label after creating it, set its offset and alignment, or use the
text attribute directly, for example panellabel.text.set_fontsize(10).
Attributes:
| Name | Type | Description |
|---|---|---|
ax |
PanelAxes
|
The panel the label belongs to. |
xoffset |
float
|
Horizontal offset from the panel's upper left corner, in cm. |
yoffset |
float
|
Vertical offset from the panel's upper left corner, in cm. |
text |
Text
|
The text object. |
Examples:
Create a panel with a label, then move the label and align it to its right edge:
>>> import scilayout
>>> fig = scilayout.figure()
>>> ax = fig.add_panel((3, 2, 4, 3), method="bounds", panellabel="a")
>>> ax.panellabel.set_offset(x=-0.8, y=-0.4) # cm from the panel's corner
>>> ax.panellabel.set_alignment(h="right")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
PanelAxes
|
The panel to label. |
required |
label
|
str
|
The label text. Changed to upper or lower case if the "panellabel.case" style parameter is set. |
required |
anchorlocation
property
anchorlocation: tuple
The (x, y) location of the panel's upper left corner, in cm.
get_location
get_location() -> tuple[centimetres, centimetres]
Get the position of the label on the figure.
Returns:
| Type | Description |
|---|---|
tuple of float
|
(x, y) in cm, origin at the upper left of the figure. |
set_location
set_offset
Set the position of the label relative to the panel's upper left corner.
The offset is kept, so the label follows the panel when it is moved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Distance (cm) to offset the label horizontally. Negative values move it left. Unchanged if None. |
None
|
y
|
float
|
Distance (cm) to offset the label vertically. Negative values move it up. Unchanged if None. |
None
|
set_alignment
set_alignment(
h: Literal["left", "center", "right"] = "left",
v: Literal[
"baseline",
"bottom",
"center",
"center_baseline",
"top",
] = "baseline",
) -> None
Align the label text to its position.
The position is the panel's upper left corner plus the offset (see
set_offset). The alignment says
which point of the text sits there: with h="right" the right edge of the
text is at the position, so the label grows to the left, away from the panel.
The defaults are the ones labels start with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
('left', 'center', 'right')
|
Horizontal alignment, "left" by default. |
"left"
|
v
|
('baseline', 'bottom', 'center', 'center_baseline', 'top')
|
Vertical alignment, "baseline" by default. |
"baseline"
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
FigureText
FigureText(
x: centimetres,
y: centimetres,
text: str,
figure: Figure,
**kwargs: Any,
)
Text positioned on the figure in cm from its upper left corner.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
Text
|
The text object. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Horizontal position in cm from the left of the figure. |
required |
y
|
float
|
Vertical position in cm from the top of the figure. |
required |
text
|
str
|
The text to show. |
required |
figure
|
Figure
|
The figure to add the text to. |
required |
**kwargs
|
Any
|
Keyword arguments for |
{}
|
set_position
set_position(x: centimetres, y: centimetres) -> None