Expression tracks¶
See expression one cell at a time. Each track is one gene. Bar height shows expression; color identifies the cell group.
Install or update with pip install --upgrade 'genome-spy-python[scanpy]'. Choose that environment as your Jupyter kernel.
import scanpy as sc
import genome_spy.integrations.scanpy as gsc
pbmc = sc.datasets.pbmc68k_reduced()
genes = ["CD79A", "MS4A1", "CST3", "LYZ", "CD3D", "IL32"]
Plot individual cells¶
Hover a bar for its cell, group and expression. Scroll to zoom across cells and drag to pan. All tracks stay aligned.
tracks = gsc.pl.tracksplot(
pbmc,
genes,
"bulk_labels",
zoom=True,
width=440,
height=240,
show=False,
)
tracks
Order groups with a dendrogram¶
Compute the group tree once with Scanpy. The plot reuses it. A marker dictionary groups the gene tracks with a colored strip on the right.
sc.tl.dendrogram(pbmc, groupby="bulk_labels")
markers = {
"B cells": ["CD79A", "MS4A1"],
"Myeloid": ["CST3", "LYZ"],
"T cells": ["CD3D", "IL32"],
}
grouped_tracks = gsc.pl.tracksplot(
pbmc,
markers,
"bulk_labels",
dendrogram=True,
zoom=True,
width=440,
height=240,
show=False,
)
grouped_tracks
PBMC3k with space between groups¶
Adapt the Marsilea tracks example using 2,638 PBMC3k cells and the same 12 markers. Scanpy computes the group tree from these markers.
group_gap=3 separates cell groups. spacing=6 adds room between gene tracks. Both use pixels and default to zero. The tracks, tree and color strip stay aligned as you zoom.
pbmc3k_markers = {
"CD4 T cells": ["IL7R"],
"CD14+ Monocytes": ["CD14", "LYZ"],
"B cells": ["MS4A1"],
"CD8 T cells": ["CD8A"],
"NK cells": ["GNLY", "NKG7"],
"FCGR3A+ Monocytes": ["FCGR3A", "MS4A7"],
"Dendritic cells": ["FCER1A", "CST3"],
"Megakaryocytes": ["PPBP"],
}
pbmc3k_genes = [gene for group in pbmc3k_markers.values() for gene in group]
pbmc3k = sc.datasets.pbmc3k_processed()
pbmc3k.uns["louvain_colors"] = [
"#568564",
"#DC6B19",
"#F72464",
"#005585",
"#9876DE",
"#405559",
"#58DADA",
"#F85959",
]
sc.tl.dendrogram(pbmc3k, groupby="louvain", var_names=pbmc3k_genes, use_raw=True)
spaced_tracks = gsc.pl.tracksplot(
pbmc3k,
pbmc3k_markers,
"louvain",
dendrogram=True,
zoom=True,
group_gap=3,
spacing=6,
width=440,
height=420,
show=False,
)
spaced_tracks
Choose values and size¶
These plots use the dataset’s raw expression snapshot. Use use_raw=False for X, or layer="counts" when your data has a counts layer.
width sets the plot width. height sets the combined height of the gene tracks. Set zoom=False or hover=False to turn off those interactions.
Bars give stepped profiles. Scanpy fills between neighboring cells. See supported options for the supported options.
Data use and provenance
The first examples use 700 preprocessed PBMCs from 10x Genomics, licensed CC BY 4.0. The last example uses Scanpy’s processed PBMC3k dataset, also CC BY 4.0. Its markers and palette follow the linked Marsilea tutorial. Credit 10x Genomics and Scanpy’s preprocessing contributors. Scanpy computes group trees from supplied PCA coordinates or the explicitly selected PBMC3k markers. Expression values and cell annotations are retained.