plotting#

scportrait.plotting.add_scalebar

Add a scalebar to an axis.

scportrait.plotting.colorize

Create an RGB image from a single-channel image using a specified color.

scportrait.plotting.generate_composite

Create a composite image from a multi-channel image for visualization.

sdata#

scportrait.plotting.sdata.plot_image(sdata: SpatialData, image_name: str, coordinate_systems: str | list[str] | None = None, channel_names: list[str] | list[int] | None = None, palette: list[str] | None = None, cmap: ListedColormap | None = None, title: str | None = None, title_fontsize: int = 20, dpi: int | None = None, norm=None, ax: Axes = None, return_fig: bool = False, show_fig: bool = True) Figure | None#

Plot the image with the given name from the spatialdata object.

Note

Requires a working installation of spatialdata_plot. Can be installed via pip install spatialdata_plot or is shipped with scPortrait when installing with the plotting extra (pip install scportrait[plotting]).

Parameters:
  • sdata – SpatialData object containing the image.

  • image_name – Name of the image to plot.

  • coordinate_systems – Coordinate system(s) to plot. If None, all coordinate systems are plotted.

  • channel_names – List of channel names to plot. If None then all channels will be plotted. Defaults to None.

  • palette – List of colors to use for the channels. If None then the default palette will be used. Defaults to None.

  • title – Title of the plot. Defaults to None.

  • title_fontsize – Font size of the title in points. Defaults to 20.

  • dpi – Dots per inch of the plot. Defaults to None.

  • ax – Matplotlib axis object to plot on. Defaults to None.

  • return_fig – Whether to return the figure. Defaults to False.

  • show_fig – Whether to show the figure. Defaults to True.

Returns:

Matplotlib figure object if return_fig is True.

Examples

>>> from spatialdata.datasets import blobs
>>> from scportrait.plotting import sdata as splot
>>> sdata = blobs()
>>> fig = splot.plot_image(
...     sdata=sdata,
...     image_name="blobs_image",
...     channel_names=[0],
...     palette=["red"],
...     return_fig=True,
...     show_fig=False,
... )
scportrait.plotting.sdata.plot_segmentation_mask(sdata: SpatialData, masks: list[str], background_image: str | None = 'input_image', selected_channels: int | list[int] | None = None, max_channels_to_plot: int = 4, title: str | None = None, title_fontsize: int = 20, line_width: int = 1, line_color: str = 'white', line_alpha: float = 1, fill_alpha: float = 0, fill_color: str | None = None, dpi: int | None = None, ax: Axes | None = None, return_fig: bool = False, show_fig: bool = True) Figure | None#

Visualize segmentation masks over selected background image. Will transform the provided labels layers to polygons and plot them over the background image.

Note

Requires a working installation of spatialdata_plot. Can be installed via pip install spatialdata_plot or is shipped with scPortrait when installing with the plotting extra (pip install scportrait[plotting]).

Parameters:
  • sdata – SpatialData object containing the input image and segmentation mask.

  • masks – List of keys identifying the segmentation masks to plot.

  • background_image – Key identifying the background image to plot the segmentation masks on. Defaults to “input_image”. If set to None then only the segmentation masks will be plotted as outlines.

  • selected_channels – Index or list of indices of background-image channels to plot. If None then the first max_channels_to_plot channels will be plotted. Defaults to None.

  • title – Title of the plot. Defaults to None.

  • title_fontsize – Font size of the title in points.

  • line_width – Width of the outline of the segmentation masks.

  • line_color – Color of the outline of the segmentation masks.

  • line_alpha – Alpha value of the outline of the segmentation masks.

  • fill_alpha – Alpha value of the fill of the segmentation masks.

  • fill_color – Color of the fill of the segmentation masks. If None then no fill will be used. Defaults to None.

  • dpi – Dots per inch of the plot. Defaults to None.

  • ax – Matplotlib axis object to plot on. Defaults to None.

  • return_fig – Whether to return the figure. Defaults to False.

  • show_fig – Whether to show the figure. Defaults to True.

Examples

>>> from spatialdata.datasets import blobs
>>> from scportrait.plotting import sdata as splot
>>> sdata = blobs()
>>> fig = splot.plot_segmentation_mask(
...     sdata=sdata,
...     masks=["blobs_labels"],
...     background_image="blobs_image",
...     selected_channels=0,
...     return_fig=True,
...     show_fig=False,
... )
scportrait.plotting.sdata.plot_shapes(sdata: SpatialData, shapes_layer: str | None = None, label_layer: str | None = None, coordinate_systems: str | list[str] | None = None, method: str | None = None, title: str | None = None, title_fontsize: int = 20, fill_color: str = 'grey', fill_alpha: float = 1, outline_color: str = 'black', outline_alpha: float = 0, outline_width: float = 1, cmap: str = None, palette: dict | list | None = None, groups: list | None = None, dpi: int | None = None, ax: Axes | None = None, return_fig: bool = False, show_fig: bool = True) Figure | None#

Visualize a shapes layer.

Note

Requires a working installation of spatialdata_plot. Can be installed via pip install spatialdata_plot or is shipped with scPortrait when installing with the plotting extra (pip install scportrait[plotting]).

Parameters:
  • sdata – SpatialData object containing the shapes or labels layer.

  • shapes_layer – Key identifying the shapes layer to plot.

  • label_layer – Key identifying a labels layer to convert to shapes and plot.

  • coordinate_systems – Coordinate system(s) to plot. If None, all coordinate systems are plotted.

  • method – Plotting backend passed to spatialdata_plot (None, “matplotlib”, or “datashader”).

  • title – Title of the plot.

  • title_fontsize – Font size of the title in points.

  • fill_color – Color of the fill of the shapes.

  • fill_alpha – Alpha value of the fill of the shapes.

  • outline_color – Color of the outline of the shapes.

  • outline_alpha – Alpha value of the outline of the shapes.

  • outline_width – Width of the outline of the shapes.

  • cmap – Colormap to use for the shapes.

  • palette – Palette for discrete annotations.

  • groups – Groups to plot when the color key refers to discrete annotations.

  • dpi – Dots per inch of the plot.

  • ax – Matplotlib axis object to plot on.

  • return_fig – Whether to return the figure.

  • show_fig – Whether to show the figure.

Returns:

Matplotlib figure object if return_fig is True.

Examples

>>> from spatialdata.datasets import blobs
>>> from scportrait.plotting import sdata as splot
>>> sdata = blobs()
>>> fig = splot.plot_shapes(
...     sdata=sdata,
...     shapes_layer="blobs_polygons",
...     return_fig=True,
...     show_fig=False,
... )
scportrait.plotting.sdata.plot_labels(sdata: SpatialData, label_layer: str, coordinate_systems: str | list[str] | None = None, method: str | None = None, title: str | None = None, title_fontsize: int = 20, color: str = 'grey', fill_alpha: float = 1, cmap: str = None, palette: dict | list | None = None, groups: list | None = None, norm: Normalize = None, vectorized: bool = False, dpi: int | None = None, ax: Axes | None = None, return_fig: bool = False, show_fig: bool = True) Figure | None#

Plot a labels layer.

Note

Requires a working installation of spatialdata_plot. Can be installed via pip install spatialdata_plot or is shipped with scPortrait when installing with the plotting extra (pip install scportrait[plotting]).

Parameters:
  • sdata – SpatialData object containing the input image and segmentation mask.

  • label_layer – Key identifying the label layer to plot.

  • coordinate_systems – Coordinate system(s) to plot. If None, all coordinate systems are plotted.

  • method – Plotting backend passed to spatialdata_plot (None, “matplotlib”, or “datashader”).

  • title – Title of the plot.

  • title_fontsize – Font size of the title in points.

  • color – Color to plot the label layer in. Can be a string specifying a specific color or linking to a column in an annotating table.

  • fill_alpha – Alpha value of the fill of the segmentation masks.

  • cmap – Colormap to use for the labels.

  • palette – Palette for discrete annotations. List of valid color names that should be used for the categories. Must match the number of groups. The list can contain multiple palettes (one per group) to be visualized. If groups is provided but not palette, palette is set to default “lightgray”.

  • groups – When using color and the key represents discrete labels, groups can be used to show only a subset of them. Other values are set to NA.

  • norm – Colormap normalization for continuous annotations.

  • vectorized – Whether to plot a vectorized version of the labels.

  • dpi – Dots per inch of the plot.

  • ax – Matplotlib axis object to plot on.

  • return_fig – Whether to return the figure.

  • show_fig – Whether to show the figure.

Returns:

Matplotlib figure object if return_fig is True.

Examples

>>> from spatialdata.datasets import blobs
>>> from scportrait.plotting import sdata as splot
>>> sdata = blobs()
>>> fig = splot.plot_labels(
...     sdata=sdata,
...     label_layer="blobs_labels",
...     color="labelling_categorical",
...     return_fig=True,
...     show_fig=False,
... )

h5sc#

Collection of plotting functions for scPortrait’s standardized single-cell image format

scportrait.plotting.h5sc.cell_grid_single_channel(adata, select_channel: int | str, mask_id: int | None = None, n_cells: int = 16, cell_ids: int | list[int] | None = None, cell_labels: list[str] | None = None, show_cell_id: bool = False, title: str | None = None, show_title: bool = True, title_rotation: float = 0, cmap='viridis', ncols: int | None = None, nrows: int | None = None, single_cell_size: int = 2, vmin: float = 0, vmax: float = 1, spacing: float = 0.025, ax: Axes = None, return_fig: bool = False, show_fig: bool = True) None | Figure#

Visualize a single channel from h5sc object in a grid.

Parameters:
  • adata – An scPortrait single-cell image dataset.

  • select_channel – The channel to visualize.

  • mask_id – The index of a mask to visualize as outlines.

  • n_cells – The number of cells to visualize. This number of cells will randomly be selected. If None, cell_ids must be provided.

  • cell_ids – cell IDs for the specific cells that should be visualized. If None, n_cells must be provided.

  • cell_labels – Label to plot as title for each single-cell image if provided.

  • show_cell_id – Whether to show the cell ID as title for each single-cell image. Can not be used together with cell_labels.

  • title – The title of the plot.

  • show_title – Whether to show the title.

  • title_rotation – The rotation of the title in degrees.

  • cmap – The colormap to use for the images.

  • ncols – The number of columns in the grid. If not specified will be automatically calculated to make a square grid.

  • nrows – The number of rows in the grid. If not specified will be automatically calculated to make a square grid.

  • single_cell_size – The size of each cell in the grid.

  • spacing – The spacing between cells in the grid expressed as fraction of the cell image size.

  • ax – The matplotlib axes object to plot on. If None, a new figure is created.

  • return_fig – If True, the function returns the figure object instead of displaying it.

  • show_fig – If True, the figure is displayed.

Returns:

If return_fig=True, the figure object is returned. Otherwise, the figure is displayed.

scportrait.plotting.h5sc.cell_grid_multi_channel(adata, n_cells: int = 5, cell_ids: int | list[int] | None = None, select_channels: list[int] | list[str] | None = None, title: str | None = None, show_cell_id: bool = True, label_channels: bool = True, channel_label_rotation: float = 0, row_labels: list[str] | None = None, cmap='viridis', spacing: float = 0.025, single_cell_size: int = 2, ax: Axes = None, return_fig: bool = False, show_fig: bool = True) None | Figure#

Plot a grid of single-cell images where each row is a unique cell and each column contains a different channel.

Parameters:
  • adata – An scPortrait single-cell image dataset.

  • n_cells – The number of cells to visualize. This number of cells will randomly be selected. If None, cell_ids must be provided.

  • cell_ids – cell IDs for the specific cells that should be visualized. If None, n_cells must be provided.

  • select_channels – The channels to visualize. Channels are identified as int values corresponding to their order in the h5sc dataset. If None, all channels will be visualized.

  • title – The title of the plot.

  • show_cell_id – Whether to show the cell ID as row label for each cell in the image grid.

  • label_channels – Whether to show the channel names as titles for column in the image grid.

  • channel_label_rotation – The rotation of the channel labels in degrees.

  • row_labels – can override the labels plotted on the y-axis on the first element of each row. If using not compatible with passing show_cell_id as True.

  • cmap – The colormap to use for the images.

  • spacing – The spacing between cells in the grid expressed as fraction of the cell image size.

  • single_cell_size – The size of each cell in the grid.

  • ax – The matplotlib axes object to plot on. If None, a new figure is created.

  • return_fig – If True, the function returns the figure object instead of displaying it.

  • show_fig – If True, the figure is displayed.

Returns:

If return_fig=True, the figure object is returned. Otherwise, the figure is displayed.

scportrait.plotting.h5sc.cell_grid(adata: AnnData, select_channel: int | str | list[int] | list[str] = None, n_cells: int | None = None, cell_ids: int | list[int] | None = None, show_cell_id: bool = True, cmap='viridis', return_fig: bool = False, show_fig: bool = True)#

Visualize single-cell images of cells in an AnnData object.

Uses either cell_grid_single_channel or cell_grid_multi_channel depending on the input. Use these functions if you would like more control over the visualization.

Parameters:
  • adata – An scPortrait single-cell image dataset.

  • select_channel – The channel to visualize. Can be either a single int value or a list of values. If None all channels will be visualized.

  • n_cells – The number of cells to visualize. This number of cells will randomly be selected. If set to None, cell_ids must be provided.

  • cell_ids – cell IDs of the cells that are to be visualiazed. If None, n_cells must be provided.

  • show_cell_ids – Whether to show the cell ID as titles/y-labels for each single-cell image.

  • cmap – The colormap to use for the images.

  • return_fig – If True, the function returns the figure object instead of displaying it.

  • show_fig – If True, the figure is displayed.

Returns:

If return_fig=True, the figure object is returned. Otherwise, the figure is displayed.