neurocarto.views.image_plt

class neurocarto.views.image_plt.PltImageView

Use matplotlib to generate an image as a background image.

Example:

class PlotChannelMap(PltImageView):
    def on_probe_update(self, probe, chmap, e):
        if chmap is not None:
            self.plot_channelmap(chmap)
        else:
            self.set_image(None)

    def plot_channelmap(self, m):
        from chmap.probe_npx import plot

        with self.plot_figure() as ax:
            plot.plot_channelmap_block(ax, chmap=m)
            plot.plot_probe_shape(ax, m, color='k')
__init__(config, *, logger='neurocarto.view.plt')
Parameters:
property name: str

view name

set_image_handler(image)
Parameters:

image (ImageHandler | None)

set_image(image, boundary=None, offset=0)

Set image. Due to the figure origin point usually not the origin point in axes, you need to provide boundary to tell program how to align the image with the probe.

Parameters:
  • image (ndarray[tuple[int, ...], dtype[uint64]] | None) – image array. May from get_current_plt_image(). Use None to clear image.

  • boundary (Boundary | None) – image boundary. May from get_current_plt_boundary()

  • offset (float | tuple[float, float]) – x or (x, y) offset with unit um. When you don’t want the image overlapped with other graphics.

restore_state(state)

Restore state from state.

Parameters:

state (PltImageState) – json-deserializable instance.

start()

Invoked when figure is ready.

plot_figure(nrows: int = 1, ncols: int = 1, *, sharex: bool | Literal['none', 'all', 'row', 'col'] = False, sharey: bool | Literal['none', 'all', 'row', 'col'] = False, squeeze: bool = True, width_ratios: Sequence[float] | None = None, height_ratios: Sequence[float] | None = None, subplot_kw: dict[str, Any] | None = None, gridspec_kw: dict[str, Any] | None = None, offset: float | tuple[float, float] = 0, transparent: bool = True, rc: str = None, **kwargs) ContextManager[Axes]

A context manager of handle a matplotlib axes to capture the graphics.

with self.plot_figure() as ax:
    ax.plot(...)

Once context closed, set_image() is called with parameters image, boundary and offset automatically.

If a KeyboardInterrupt is raised, capture it and clear the current image.

If an error except a KeyboardInterrupt is raised, reraise it and do nothing on the current image.

Parameters:
  • transparent – pass to fig.savefig(transparent).

  • rc – matplotlib rc filename (str or Path). default is read from the file image_plt.matplotlibrc.

  • offset – see offset in set_image()

  • kwargs – pass to plt.subplots(kwargs)

Returns:

a context manger that carries an Axes

class neurocarto.views.image_plt.Boundary

Matplotlib axes boundary in a figure.

shape: tuple[int, int]

Figure size in pixels.

bbox: tuple[float, float, float, float]

Axes boundary box (left, bottom, right, top) in ratio.

xlim: tuple[float, float]

Axes x-axis limits

ylim: tuple[float, float]

Axes y-axis limits

as_um()

Change value unit from mm to um.

Return type:

Self

property fg_width_px: int

Figure width in pixel

property fg_height_px: int

Figure height in pixel

property fg_width: float

Figure width in um

property fg_height: float

Figure height in um.

property fg_xlim: tuple[float, float]

Extended x-axis limits for the figure.

property fg_ylim: tuple[float, float]

Extended y-axis limits for the figure.

property fg_extent: tuple[float, float, float, float]

Figure’s extent.

Returns:

(left, bottom, right, top) in um

property ax_width: float

Axes width in um.

property ax_height: float

Axes height in um.

property ax_width_px: int

Axes width in pixel

property ax_height_px: int

Axes height in pixel

property ax_extent: tuple[float, float, float, float]

Axes’s extent.

Returns:

(left, bottom, right, top) in um

property ax_extent_px: tuple[int, int, int, int]
Returns:

(left, bottom, right, top) in pixels

property origin_px: tuple[int, int]

origin point position in figure.

Returns:

(x, y) pixel

point_px(x, y)

point position in figure.

Parameters:
  • x (float) – x coordinate in axes

  • y (float) – y coordinate in axes

Returns:

(x, y) pixel

Return type:

tuple[int, int]

property center: tuple[float, float]

The center position of the figure.

Returns:

(x, y) in um

point(x, y)

The point position of the figure.

Parameters:
  • x (int) – x coordinate on figure in pixels.

  • y (int) – y coordinate on figure in pixels.

Returns:

(x, y) in um

Return type:

tuple[float, float]

property scale: tuple[float, float]

scale for (x, y) in unit: um/pixels

property scale_x: float
property scale_y: float
static __new__(_cls, shape, bbox, xlim, ylim)

Create new instance of Boundary(shape, bbox, xlim, ylim)

Parameters:
  • shape (tuple[int, int])

  • bbox (tuple[float, float, float, float])

  • xlim (tuple[float, float])

  • ylim (tuple[float, float])

neurocarto.views.image_plt.get_current_plt_image(fg=None, **kwargs)

Save a matplotlib figure into a numpy array.

Parameters:
  • fg – matplotlib Figure. Use plt.gcf() when None.

  • kwargs – pass to fg.savefig(kwargs), except parameters format and dpi.

Returns:

a numpy array.

Return type:

ndarray[tuple[int, …], dtype[uint64]]

neurocarto.views.image_plt.get_current_plt_boundary(ax=None)

Get Axes boundary.

Parameters:

ax (Axes) – matplotlib Axes. Use plt.gca() when None.

Returns:

a boundary

Return type:

Boundary