Violin Plots for Gene Expression: Seurat and Scanpy Guide

How to draw and read gene expression violin plots in Seurat and Scanpy, including stacked violins and zero-heavy single-cell data.

In single-cell and bulk RNA-seq, a violin plot shows how a gene’s expression is spread across the cells or samples in each group, such as clusters or conditions. In R, Seurat’s VlnPlot() draws them. In Python, Scanpy’s sc.pl.violin() and sc.pl.stacked_violin() do the same job. The tricky part is reading them, because single-cell values are full of zeros and are usually log-normalised.

For a quick figure from an exported table of values, you can also paste the numbers into our free violin plot maker and download the chart.

Why violins are popular for expression data

A cluster of a few thousand cells rarely has a neat bell-shaped expression profile. Often a share of cells express a gene strongly and the rest show nothing. A bar chart of means hides that completely. A boxplot can collapse to a line at zero if more than half the cells are zero. A violin shows both parts: the bulge at zero and the shape of the expressing cells above it. For the general idea, see violin plot vs box plot.

Seurat: VlnPlot

VlnPlot() takes a Seurat object and one or more features. By default it groups cells by their current identity, which is usually the cluster labels.

library(Seurat)

# marker genes across clusters
VlnPlot(pbmc, features = c("MS4A1", "CD79A"))

# group by a metadata column instead, and hide the points
VlnPlot(pbmc, features = "CD3E", group.by = "orig.ident", pt.size = 0)

# compare conditions within each cluster
VlnPlot(pbmc, features = "IFIT1", split.by = "condition", pt.size = 0)

Arguments worth knowing:

  • features: genes, or anything FetchData() can return, such as QC metrics or PC scores.
  • group.by: a metadata column to group by instead of the active identity.
  • split.by: a second factor, such as condition, drawn within each group.
  • pt.size: the size of the cell points. With thousands of cells, pt.size = 0 usually gives a cleaner plot.
  • layer: which data layer to plot. In Seurat v5 this replaces the deprecated slot. The default is the normalised data. layer = "counts" plots raw counts.
  • log: puts the expression axis on a log scale.
  • stack and flip: draw many genes as one compact stacked figure (see below).
  • add.noise: on by default. It adds a small amount of noise to the values for plotting. Set it to FALSE if you want the exact values drawn.

Violins are also the standard QC view in Seurat workflows, showing genes per cell, counts per cell and mitochondrial percentage:

pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)

Because VlnPlot() returns a ggplot (or a patchwork of them), you can add ggplot2 layers and themes. To go further with ggplot2’s own geom_violin(), see our violin plot in R guide.

Scanpy: sc.pl.violin

Scanpy works on an AnnData object. keys are gene names from adata.var_names or columns of adata.obs. groupby is a categorical column in adata.obs, such as cluster labels.

import scanpy as sc

adata = sc.datasets.pbmc68k_reduced()  # small example dataset

# two genes across cell types, with jittered cells on top
sc.pl.violin(adata, keys=["CD79A", "NKG7"], groupby="bulk_labels",
             jitter=0.4, stripplot=True, size=1, rotation=90)

# no points, which is faster for large datasets
sc.pl.violin(adata, keys="CST3", groupby="louvain", stripplot=False)

# QC metrics, each in its own panel
sc.pl.violin(adata, keys=["n_genes", "percent_mito"],
             jitter=0.4, multi_panel=True)
  • stripplot (default True) overlays the individual cells. jitter spreads them sideways, and size sets the dot size.
  • use_raw defaults to using adata.raw when it exists. layer picks a named layer and takes precedence over use_raw.
  • density_norm sets how widths compare: "width" (the default here), "area" or "count". Older Scanpy code calls this scale.
  • log=True uses a log axis, and order fixes the group order.

Scanpy draws violins with seaborn, so extra keyword arguments pass through to seaborn.violinplot(). Our violin plot in Python guide covers those options.

Stacked violins for many genes

One violin panel per gene gets unwieldy past a handful of markers. A stacked violin plot puts genes along one axis and groups along the other, with a small violin in each cell of the grid. It is a compact way to show a marker panel across clusters.

markers = ["CD79A", "MS4A1", "CD3D", "NKG7", "CST3", "LYZ", "FCGR3A"]
sc.pl.stacked_violin(adata, markers, groupby="bulk_labels")

# group the genes under labels
marker_groups = {"B": ["CD79A", "MS4A1"],
                 "T/NK": ["CD3D", "NKG7"],
                 "Myeloid": ["CST3", "LYZ"]}
sc.pl.stacked_violin(adata, marker_groups, groupby="bulk_labels")

By default each violin is coloured by the median expression in that group, which adds a heatmap-like cue. swap_axes=True flips the layout, and dendrogram=True orders the groups by similarity.

In Seurat, the equivalent is stack = TRUE:

markers <- c("CD79A", "MS4A1", "CD3D", "NKG7", "CST3", "LYZ")
VlnPlot(pbmc, features = markers, stack = TRUE, flip = TRUE) + NoLegend()

Reading zero-inflated distributions

Most genes are detected in only some cells, partly because of real biology and partly because of how little RNA is captured per cell. On a violin, that shows up as a wide base sitting on zero. Some practical rules:

  • Read the base and the body separately. The width at zero tells you roughly what share of cells have no detected expression. The shape above zero tells you how strongly the expressing cells express the gene.
  • A taller violin is not always a higher-expressing group. One group can have a higher maximum but far fewer expressing cells. Check the base too.
  • Width scaling changes the impression. With width scaling, every group has the same maximum width, so a group of 50 cells looks as big as one of 5,000. Note cell counts in the figure or legend.
  • The density can leak below zero. Smoothing a spike at zero can draw a sliver below the axis. That is an artefact of the kernel density estimate, not negative expression.
  • Few expressing cells give lumpy shapes. If only a dozen cells express the gene, the upper part of the violin is mostly smoothing. Show the points, or use a dot plot that reports the fraction expressing directly.

Smoothing matters a lot with spiky data like this. The violin plot bandwidth guide shows how the shape shifts as smoothing changes. For general reading tips, see how to read a violin plot.

What the y axis means: log-normalised values

In both standard workflows, the default values on the y axis are not raw counts.

  • Seurat: NormalizeData() with the default LogNormalize method divides each cell’s counts by its total, multiplies by a scale factor of 10,000, and takes the natural log of the value plus one.
  • Scanpy: the common recipe is sc.pp.normalize_total(adata, target_sum=1e4) followed by sc.pp.log1p(adata), which gives values on a similar scale.

So a value of 2 is not “2 molecules”. It is the log of a depth-normalised count. Differences on this axis are roughly differences in log scale, which is why the shapes look less skewed than raw counts would. Label the axis to say so, for example “log-normalised expression”.

Be careful with scaled data. Seurat’s ScaleData() and Scanpy’s sc.pp.scale() centre each gene to a mean of zero. Violins of scaled values have negative values by design and no longer show the zero spike in the same way. They suit heatmaps more than violins. Also check whether Scanpy is plotting adata.raw or adata.X, since use_raw defaults to the raw slot when it exists.

Violins and statistics

A violin plot is a picture of the distribution, not a test. Treating each cell as an independent replicate when comparing conditions can overstate significance, because cells from the same sample are not independent. If you compare conditions, do it at the level of biological samples, for example with pseudobulk methods, and use violins to show the data rather than to prove a difference.

For background on the chart itself, read the violin plot guide. If you have a small table of expression values for a talk or report, you can make a violin plot online without loading the full dataset.

FAQ

Why is there a big bulge at zero in my violin plot?

Many cells have no detected reads for that gene. The width at zero reflects the share of non-expressing cells, which is normal in single-cell data.

How do I hide the dots in Seurat’s VlnPlot?

Set pt.size = 0. In Scanpy, set stripplot=False.

What values does VlnPlot show by default?

The normalised data layer, which after NormalizeData() is log-normalised expression. Use layer = "counts" for raw counts.

When should I use a stacked violin plot?

When you want to show many marker genes across many groups in one figure. Use sc.pl.stacked_violin() in Scanpy or VlnPlot(..., stack = TRUE) in Seurat.

Adrian Lee
Adrian Lee

Adrian Lee is the Data Visualization Specialist and Statistical Analyst at ViolinPlot. Based in Boston, Massachusetts, Adrian utilizes a robust background in statistics and data science to create compelling and insightful visualizations that help interpret complex data.

With extensive experience in data analysis and visualization techniques, Adrian specializes in developing violin plots and other advanced graphical representations to convey statistical information effectively. Adrian’s expertise includes statistical modeling, data interpretation, and the creation of interactive visualizations.

At ViolinPlot, Adrian is responsible for transforming raw data into visual narratives that are both informative and engaging.

By employing state-of-the-art visualization tools and statistical methods, Adrian ensures that the visual representations are accurate, clear, and valuable for decision-making processes.

Adrian’s dedication to enhancing data comprehension through visual tools has established ViolinPlot. As a leading resource for data visualization and statistical analysis.

Articles: 89

Ready to Unlock Deeper Data Insights?

Transform complex datasets into clear, interactive visualizations with Violin Plot. Start exploring data distributions in a way that empowers insight and drives smarter decisions.

LEARN MOre
100% satisfaction guaranteed
McAfee SECURE
ssl-secured
verisign-secured
BEST-PRICE-GUARANTEED-free-trust-badge
guaranteed-safe-checkout-1