Skip to content

striqt.figures.backend

The plot backend protocol and its matplotlib and plotly implementations.

plotting backends that draw the figures of each data variable.

MatplotlibBackend

MatplotlibBackend(
    opts: SharedPlotOptions,
    output_dir: Path | None,
    *,
    interactive: bool | str | None = None,
    lock: RLock | None = None,
)

Bases: PlotBackend['xarray.plot.FacetGrid']

a plotting backend that draws with matplotlib, one FacetGrid per figure.

Each drawing method lays out the facets from opts and returns the grid. An image or line of more than 10000 values, and every band, is rasterized. A line drawn onto a target adds no legend entry.

Parameters:

  • opts (SharedPlotOptions) –

    the facet layout and label formats of every figure

  • output_dir (Path | None) –

    the directory for the figure files; None writes no file

  • interactive (bool | str | None, default: None ) –

    True leaves pyplot interactive after each finish; a TerminalGraphics name shows each figure in the terminal; False or None does neither

  • lock (RLock | None, default: None ) –

    serializes the terminal image writes across processes; None locks nothing

output_dir instance-attribute

output_dir: Path | None = output_dir

the directory of the figure files written by finish; None writes no file

interactive instance-attribute

interactive: bool | str | None = interactive

True leaves pyplot interactive after finish; a TerminalGraphics name shows each figure in the terminal; False or None does neither

lock instance-attribute

lock: RLock | _FakeLock

serializes the terminal image writes of finish across processes

last_grid class-attribute instance-attribute

last_grid: FacetGrid | None = None

the grid of the most recent finish call, or None before the first

finish

finish(
    target: FacetGrid,
    xticklabelunits: bool | Literal['auto'] = 'auto',
) -> Path | None

label target from the attrs of the drawn data and the formats of opts.

With output_dir set, the figure file named by opts.filename_fmt is written there at the dpi between 120 and 300 that resolves each sample of the drawn data, and a record is logged on the analysis logger at INFO. Interactive pyplot mode is turned on when interactive is True. The figure is shown in the terminal and closed when interactive names a terminal graphics protocol. target is kept as last_grid.

Parameters:

  • target (FacetGrid) –

    the grid returned by a drawing method of this backend

  • xticklabelunits (bool | Literal['auto'], default: 'auto' ) –

    whether each x tick label ends with the units; 'auto' selects True for a units attr of 1 or 2 characters

Returns:

  • Path | None –

    the path of the figure file, or None when output_dir is None

PlotBackend

Bases: Protocol[T]

the drawing methods of a plotting backend with grid type T.

An implementation lays out one grid of facets per drawing call from the col, row, and col_wrap fields of opts, and returns the grid. A later call with that grid as target draws onto the same facets, and finish labels the grid.

opts instance-attribute

opts: SharedPlotOptions

the facet layout and label formats shared by every figure

line

line(
    data: DataArray,
    *,
    x: str,
    hue: str | None = None,
    yscale: ScaleOptions = 'linear',
    sharex: bool = True,
    ylim: tuple[float | None, float | None] | None = None,
    step: StepStyle | None = None,
    target: T | None = None,
) -> T

draw one line per hue value of data on each facet.

Parameters:

  • data (DataArray) –

    the values on the y axis, with dimensions x, hue (when set), and the facet dimensions of opts

  • x (str) –

    the dimension or coordinate of data on the x axis

  • hue (str | None, default: None ) –

    the dimension of data that selects one line per value, with a legend; None draws one line per facet

  • yscale (ScaleOptions, default: 'linear' ) –

    the y axis scale, 'linear' or 'log'

  • sharex (bool, default: True ) –

    whether the facets share x limits

  • ylim (tuple[float | None, float | None] | None, default: None ) –

    the y limits; None in either position leaves that limit to the data extent

  • step (StepStyle | None, default: None ) –

    the placement of the step between samples when each value is held until the next sample; None connects the samples

  • target (T | None, default: None ) –

    None builds a new grid; the lines are drawn onto a grid returned by an earlier call of this backend, with facet coordinates equal to those of data

Returns:

  • T –

    the new grid, or target when given

Raises:

  • ValueError –

    if target is a grid with facets that data does not have

fill_between

fill_between(
    low: DataArray,
    high: DataArray,
    *,
    x: str,
    hue: str | None = None,
    alpha: float = 0.25,
    step: StepStyle | None = None,
    target: T | None = None,
) -> T

shade the band from low to high per hue value on each facet.

A band and a line drawn with the same hue value on one grid have the same color.

Parameters:

  • low (DataArray) –

    the lower edge of the band, with dimensions x, hue (when set), and the facet dimensions of opts

  • high (DataArray) –

    the upper edge of the band, with the dimensions of low

  • x (str) –

    the dimension or coordinate of low and high on the x axis

  • hue (str | None, default: None ) –

    the dimension that selects one band per value; None shades one band per facet

  • alpha (float, default: 0.25 ) –

    the opacity of the band, in [0, 1]

  • step (StepStyle | None, default: None ) –

    the placement of the step between samples when each edge is held until the next sample; None connects the samples

  • target (T | None, default: None ) –

    None builds a new grid; the bands are drawn onto a grid returned by an earlier call of this backend, with facet coordinates equal to those of low

Returns:

  • T –

    the new grid, or target when given

Raises:

  • ValueError –

    if target is a grid with facets that low does not have

heatmap

heatmap(
    data: DataArray,
    *,
    x: str,
    y: str,
    cmap: str = 'cubehelix',
    vmin: float | None = None,
    vmax: float | None = None,
    vstep: float | None = None,
) -> T

draw data as a color image with a colorbar on each facet.

Parameters:

  • data (DataArray) –

    the values mapped to color, with dimensions x, y, and the facet dimensions of opts

  • x (str) –

    the dimension or coordinate of data on the x axis

  • y (str) –

    the dimension or coordinate of data on the y axis

  • cmap (str, default: 'cubehelix' ) –

    a matplotlib colormap name

  • vmin (float | None, default: None ) –

    the value at the low end of the colormap; None selects the data minimum

  • vmax (float | None, default: None ) –

    the value at the high end of the colormap; None selects the data maximum

  • vstep (float | None, default: None ) –

    the width (in the units of data) of the color bands that quantize the colormap between vmin rounded down and vmax rounded up to multiples of vstep; None maps colors continuously

Returns:

  • T –

    a new grid

finish

finish(
    target: T,
    xticklabelunits: bool | Literal['auto'] = 'auto',
) -> Path | None

label target from the attrs of the drawn data and the formats of opts.

Parameters:

  • target (T) –

    the grid returned by a drawing method of this backend

  • xticklabelunits (bool | Literal['auto'], default: 'auto' ) –

    whether each x tick label ends with the units; 'auto' leaves the choice to the backend

Returns:

  • Path | None –

    the path of the saved figure file, or None when no file is written

limit_facets

limit_facets(
    target: T,
    low: DataArray | float | None,
    high: float | None,
) -> None

set the x limits of each facet of target to (low, high).

Parameters:

  • target (T) –

    the grid to limit

  • low (DataArray | float | None) –

    the lower limit, or a DataArray with one value per facet; None leaves the lower limit to the data extent

  • high (float | None) –

    the upper limit; None leaves the upper limit to the data extent

mark_noise_level

mark_noise_level(
    data: Dataset,
    var_name: str,
    grid: T,
    where: Literal['x', 'y', 'colorbar'],
) -> None

draw the system noise level (in dBm) for var_name on each facet of grid.

The level is system_noise in the noise bandwidth of var_name, drawn on each facet at the entry for the coordinate value of that facet. Nothing is drawn when data has no system_noise variable or var_name has no noise bandwidth.

Parameters:

  • data (Dataset) –

    the dataset with the system_noise variable (dBm/Hz) and var_name

  • var_name (str) –

    the data variable or power-bin coordinate that sets the noise bandwidth

  • grid (T) –

    the grid to draw on

  • where (Literal['x', 'y', 'colorbar']) –

    'x' draws a horizontal line for power on the y axis, 'y' a vertical line for power on the x axis, and 'colorbar' a mark on the colorbar of a heatmap

Raises:

  • TypeError –

    if where is 'colorbar' and grid has no colorbar, or if where is none of the 3 values

PlotlyBackend

PlotlyBackend(opts: SharedPlotOptions)

Bases: PlotBackend['go.Figure']

the plotting backend that draws go.Figure grids for the browser.

Each drawing call without target appends one figure to a pending list that get_plot_data returns and clears. The color limits of heatmap and the y limits of line and fill_between widen across calls and narrow only at reset. sharex has no effect.

reset

reset(*, limits: bool = False) -> None

clear the pending figures.

Parameters:

  • limits (bool, default: False ) –

    True also clears the remembered color limits of heatmap and y limits of line; False keeps the remembered limits

finish

finish(
    target: Figure,
    xticklabelunits: Union[bool, Literal['auto']] = 'auto',
) -> None

leave target unchanged.

get_plot_data

get_plot_data() -> list[Figure]

return and clear the pending figures, in drawing order.

batch_term_images

batch_term_images(
    interactive: TerminalGraphics | None,
) -> Generator[None, None, None]

buffer the terminal graphics written in the context and emit the batch on exit.

The standard output of the block is collected and written as one payload; for the kitty protocol, a delete-all-images command and a cursor-home escape precede the payload, so the images of the block replace the previous ones.

coerce_column

coerce_column(data: _T, plotter: PlotBackend[Any]) -> _T

add the size-1 facet dimension _view to data of an unfaceted plot.

A plot is unfaceted when plotter.opts sets neither col nor row. With col or row set, data is returned unchanged.

select_mpl_backend

select_mpl_backend(
    style: str | None, interactive: TerminalGraphics | None
) -> None

select a matplotlib backend, with the terminal style when interactive is set.

Interactive pyplot mode is turned off.

Parameters:

  • style (str | None) –

    a matplotlib style name, or None; the terminal style is applied after it when interactive is set

  • interactive (TerminalGraphics | None) –

    'sixel', 'kitcat', or 'kitty' selects a terminal graphics backend; None selects 'agg'

Raises:

  • ValueError –

    if interactive is none of None, 'sixel', 'kitcat', or 'kitty'

term_graphics_notice

term_graphics_notice(interactive: str | None) -> None

print the terminal requirements of the graphics protocol interactive.

Nothing is printed for None.