Use the Scatterwidget with AnnData to visualize data

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)
../_images/bb28c8de45fa2dc9270e28aec3b022456b7c0b11c7b2250a8ce496161b8ab39b.png

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)
../_images/b15b4e11de00b83993dc4abbb25859c62d6b9afeaa45882521d44d56fd3570ce.png

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)
../_images/3f7080c34a6274aa9bbaf69c3a9a9438c89ca4092236b7224f9d3f5ed5b4dd52.png

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)
../_images/1372190f905b0c7803144f952aa45b0e9f05431f23ee2ac348a10519daebbe5e.png

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)
../_images/91799e16ede984ce42f9f60c33107e84cd2893bc955817fdeae5ae02020d1d02.png

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)
../_images/0838e258aa912ada4a0950549482e6f0061618ecf5ae07c182cafcfe30c08548.png

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)
../_images/3ef119b5eaa275767d1f97b81e24a8c4f8b3fe4d5852591312cd79a1cd6e839f.png

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)
../_images/63e0aac685e9fa807266e901c6dc936ae6cb04bb182e028fd462a4866db365e5.png