Plotting tips and tricks

We will use the embryo dataset analysed with the signatures based on the cell segmentation from the Celltyping tutorial as a demonstration.

Generally, all plotting functions are available as part of the sainsc.LazyKDE object and start with plot_*. They all return a matplotlib.Figure and share a few typical options and parameters including:

  • scalebars and zoom options for spatial plots

  • removing the background (most plots except the ones based on transcript counts)

  • selecting a gene instead of total mRNA for plots that show the KDE

Plotting genes

As mentioned, genes can be plotted instead of total mRNA. So instead of …

_ = embryo.plot_KDE()
../_images/4126b5fd17a52b9f71ac8486fd46c51763fda07b4f218a7380c2f44cfe0a52b4.png

… we can do …

_ = embryo.plot_KDE(gene="Ttr")
../_images/d662d752b147173182c5bbca82df2d7d02d18c19e8947c26dda2002908fd2c71.png

Zoom

Spatial plots allow zooming in by selecting a smaller region, defined by the crop parameter as ((x_min, x_max), (y_min, y_max)), to be shown.

_ = embryo.plot_KDE(gene="Ttr", crop=((4_500, 6_000), (4_200, 5_700)))
../_images/dc27caa3150ecee5e35a4ec081a6e2c66f2b4d4b9764f07cba098970df05d341.png

Remove background

Once you have filtered the background, some of the plots allow you to remove the background for ‘cleaner’ visualizations (actually, the cell-type map is visualized with the background removed by default).

roi = ((2_000, 6_000), (10_000, 15_000))
_ = embryo.plot_KDE(crop=roi)
../_images/2c7ac0a9e12329f2714011f1fd9fc4745bde97c89d150b0c01ed2d754b32c237.png

When removing the background we can see that the low-transcript areas are removed (though the background seems to not be filtered optimally and the threshold likely could be improved).

_ = embryo.plot_KDE(crop=roi, remove_background=True)
../_images/55760ff268edfb8c0bf07aca8ced80d55c6ef1de0e30aa8fe62259dff5842463.png

Scalebar

The placement and aesthetics of the scale bar can be adjusted as well. Refer to the matplotlib-scalebar package for more details.

_ = embryo.plot_KDE(scalebar_kwargs={"location": "lower left"})
../_images/843996a639bba3b2b2931485d70d2f1b7837d63a6a740e3f43e7e20df5f819b9.png

Colormaps

Sometimes it is useful to adjust the maximum value or the colormap of the plot. These parameters can usually be passed to the underlying matplotlib functions.

_ = embryo.plot_KDE(im_kwargs={"vmax": 3, "cmap": "magma"})
../_images/e1efd62567e37d6bb404d541ee120ef2901938161aa93272923d92fc6e54f7ba.png

Discrete colors for cell types

In contrast to the other plotting functions, the cell-type map is a visualization of discrete labels (rather than continuous values). You can generate a colormap either automatically by providing a palette that will be used to map the cell types to colors. Or explicitly by providing a dictionary of cell types and corresponding colors (if the color is a string, it will be converted using matplotlib.colors.to_rgb). Additionally, you can define what the background color is (black by default).

import colorcet as cc

cmap = dict(zip(embryo.celltypes, cc.glasbey))
cmap
{'Cardiomyocyte': '#d60000',
 'Chondrocyte': '#8c3bff',
 'Choroid plexus': '#018700',
 'Dorsal midbrain neuron': '#00acc6',
 'Endothelial cell': '#97ff00',
 'Epithelial cell': '#ff7ed1',
 'Erythrocyte': '#6b004f',
 'Facial fibroblast': '#ffa52f',
 'Fibroblast': '#573b00',
 'Forebrain neuron': '#005659',
 'Forebrain radial glia cell': '#0000dd',
 'Ganglion': '#00fdcf',
 'Hepatocyte': '#a17569',
 'Immune cell': '#bcb6ff',
 'Keratinocyte': '#95b577',
 'Limb fibroblast': '#bf03b8',
 'Macrophage': '#645474',
 'Meninges cell': '#790000',
 'Mid-/hindbrain and spinal cord neuron': '#0774d8',
 'Myoblast': '#fdf490',
 'Olfactory epithelial cell': '#004b00',
 'Radial glia cell': '#8e7900',
 'Smooth muscle cell': '#ff7266',
 'Spinal cord neuron': '#edb8b8',
 'Thalamus neuron': '#5d7e66'}
_ = embryo.plot_celltype_map(cmap=cmap)
../_images/4cb0cce6f89d059d90c4e52ca55dd73569898fd1f273378d1b0ff7a5828555bc.png

In the case of providing a color per cell type using a dictionary, you can also define the color to use if a cell type has not been assigned a color (referred to as undefined and grey by default). This can be useful to highlight one or a few cell types.

_ = embryo.plot_celltype_map(
    cmap={"Erythrocyte": "red", "Fibroblast": "blue"},
    undefined="grey",
)
../_images/4b9544b6848dce21472004e0c52be3f30e1683fb364971c681a35eaac5f2adfc.png

The sainsc.LazyKDE.plot_celltype_map method also allows returning the celltype map as image rather than plotting it. This can be useful if you intend to export the full-resolution image, for example.

img = embryo.plot_celltype_map(cmap=cmap, return_img=True)

img.shape
(17204, 27520, 3)