Interactive visualization with Napari

This tutorial shows how to visualize the and AnnData in Napari.

It can be useful to explore the results of Scanpy/Squidpy analysis in an interactive way. Napari is a multi-dimensional image viewer for python, which makes it very convenient for this purpose. In this tutorial, we will show how Squidpy allows a seamless integration of Napari with the AnnData object, making it very easy to interactively explore the analysis results and visualize them on the high-resolution tissue image contained in ImageContainer.

For this tutorial, you might have to install additional libraries, make sure that you installed Squidpy with pip install 'squidpy[interactive]'.

As usual, let's import relevant libraries and load the data.

In [1]:
import scanpy as sc
import squidpy as sq
import numpy as np

scanpy==1.8.0.dev78+gc488909a anndata==0.7.5 umap==0.4.6 numpy==1.20.1 scipy==1.6.0 pandas==1.2.3 scikit-learn==0.24.2 statsmodels==0.12.2 python-igraph==0.8.3 leidenalg==0.8.3
In [2]:
adata = sq.datasets.visium_hne_adata()
img = sq.datasets.visium_hne_image()

In the following line, we actually start Napari. You will notice that an additional window will pop up with the Napari viewer. We assign the Napari viewer to a new variable, viewer, in order to perform some useful manipulations, for instance taking screenshots. For users familiar with napari, the viewer is a wrapper of the napari.Viewer and not the same object.

In [3]:
viewer = img.interactive(adata)

We can always visualize the Napari session in this notebook, by conveniently capturing a screenshot of the current image. This can be done with viewer.screenshot(). All of the described functionalities are based on Napari layers and widget. You might want to familiarize yourself with some Napari tutorials.

Here, let's take a screenshot of the canvas as well as the GUI with all the available widgets.

In [4]:

Visualize observations and features from AnnData

On the right side of the Napari GUI, you can select and visualize the different slots in the AnnData object, such as:

  • Genes from the default layer adata.X or other layers such as adata.raw.
  • Observations saved in adata.obs (e.g. clustering results, QC metrics, other type of observation-level annotations).
  • Features from adata.obsm (e.g., PCA results, computed image features and other observation-level representations).

The widgets are searchable, meaning that you can type the initials of e.g. your favorite gene and the menu will automatically scroll to the location automatically. In order to visualize the feature of interest as an additional layer, you can either press Enter or double-click on it.

For instance, let's visualize the expression levels for Olig1...

In [5]: