Use the Scatterwidget with AnnData to visualize data#
This tutorial is an example of how to use the Napari-Spatialdata ScatterWidget from a notebook.
Intro: visualizing AnnData and SpatialData objects#
The scatterplot widget can be used to visualize annotations stored in AnnData
objects, both if they are part of a SpatialData
object, or if the are standalone. In this and the other example notebook we will show the case in which the AnnData
object is standalone. The SpatialData
case is analogous, with the only difference in how the widget is activated and where the data is saved.
Precisely, to activate the scatterplot widget with SpatialData
object, after opening napari-spatialdata
view Interactive(sdata)
, you can activate the scatterplot widget from the menu bar with Plugins > napari spatialdata > Scatter
. The data will be saved in the SpatialData
object.
Let’s now continue the tutorial to cover the standalone AnnData
case.
Import packages and data#
There are two options to install napari-spatialdata:
(1) Install napari
(2a) Run pip install napari-spatialdata
or,
(2b) Clone this repo and run pip install -e .
from napari_spatialdata import QtAdataScatterWidget
import squidpy as sq
import matplotlib.pyplot as plt
Load in the AnnData:
adata = sq.datasets.visium_hne_adata()
The following line is needed to initialize a widget without a QApplication:
%gui qt5
Open the ScatterWidget:
widget = QtAdataScatterWidget(adata = adata)
widget.show()
plt.rcParams['figure.facecolor'] = 'black'
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 1087.5, 899.5, -0.5)

The widget opens without any selections. You can now pick specific x-axis, y-axis, and color values to visualize in the scatterplot. Click the “Plot” button to generate the scatterplot.
Below, you can see an example where UMAP coordinates (from obsm) are used for the spatial coordinates and ‘total_count_mt’ (from ‘obs’) is used to color the points. Note that the widget to control colors changes depending on whether you choose continuous or discrete values.
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 1087.5, 899.5, -0.5)

The distribution of the color values (‘total_count_mt’) has a long tail, so you may choose to change how the values correspond to the color by using the slider on the histogram.
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 1087.5, 899.5, -0.5)

It is also possible to change the LUT (look up table) used for the visualization. Right click on the color bar to choose a new LUT. The screenshot below shows the ‘inferno’ LUT (from the set ‘local’).
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 1087.5, 899.5, -0.5)

The widget has several options to change how the data a visualized.
For example, use the right mouse button on the plot area to access “Plot Options,” where you can change the transparency of the points and add a grid. Some options may require you to click the “Plot” button for the changes to take effect.
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 1087.5, 899.5, -0.5)

If you choose to visualize discrete data (ex. ‘cluster’ from ob), a new control widget will be created.
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 1087.5, 899.5, -0.5)

The color of each label can be changed separately. Just left-click on the color patch to open the color picker.
In the example below, the color of the bottom right cluster (‘Thalamus_1’) was changed to blue.
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 1087.5, 899.5, -0.5)

If you get lost during your explorations, clicking the house icon will restore the original settings of the discrete color widget, reset the default contrast of the continuous color widget, and pan the data to the original view.
Pseudoscatter#
If you don’t provide a selection for one of the axes, the points will arrange themselves in the form of a pseudohistogram.
You can use this option to gain insight into one-dimensional distributions of your data. In the example below x-axis (None), y-axis (obs,cluster), color (obs, cluster).
You may need to wait a moment for this process to finish for very large data sets.
plt.imshow(widget.screenshot())
plt.axis('off')
(-0.5, 873.5, 893.5, -0.5)
