scanpy.pl.violin

scanpy.pl.violin(adata, keys, groupby=None, log=False, use_raw=None, stripplot=True, jitter=True, size=1, layer=None, scale='width', order=None, multi_panel=None, xlabel='', ylabel=None, rotation=None, show=None, save=None, ax=None, **kwds)

Violin plot.

Wraps seaborn.violinplot() for AnnData.

Parameters:
adata : AnnData

Annotated data matrix.

keys : Union[str, Sequence[str]]

Keys for accessing variables of .var_names or fields of .obs.

groupby : Optional[str] (default: None)

The key of the observation grouping to consider.

log : bool (default: False)

Plot on logarithmic axis.

use_raw : Optional[bool] (default: None)

Whether to use raw attribute of adata. Defaults to True if .raw is present.

stripplot : bool (default: True)

Add a stripplot on top of the violin plot. See stripplot().

jitter : Union[float, bool] (default: True)

Add jitter to the stripplot (only when stripplot is True) See stripplot().

size : int (default: 1)

Size of the jitter points.

layer : Optional[str] (default: None)

Name of the AnnData object layer that wants to be plotted. By default adata.raw.X is plotted. If use_raw=False is set, then adata.X is plotted. If layer is set to a valid layer name, then the layer is plotted. layer takes precedence over use_raw.

scale : Literal['area', 'count', 'width'] (default: 'width')

The method used to scale the width of each violin. If ‘width’ (the default), each violin will have the same width. If ‘area’, each violin will have the same area. If ‘count’, a violin’s width corresponds to the number of observations.

order : Optional[Sequence[str]] (default: None)

Order in which to show the categories.

multi_panel : Optional[bool] (default: None)

Display keys in multiple panels also when groupby is not None.

xlabel : str (default: '')

Label of the x axis. Defaults to groupby if rotation is None, otherwise, no label is shown.

ylabel : Union[str, Sequence[str], None] (default: None)

Label of the y axis. If None and groupby is None, defaults to 'value'. If None and groubpy is not None, defaults to keys.

rotation : Optional[float] (default: None)

Rotation of xtick labels.

show : Optional[bool] (default: None)

Show the plot, do not return axis.

save : Union[str, bool, None] (default: None)

If True or a str, save the figure. A string is appended to the default filename. Infer the filetype if ending on {'.pdf', '.png', '.svg'}.

ax : Optional[Axes] (default: None)

A matplotlib axes object. Only works if plotting a single component.

**kwds

Are passed to violinplot().

Returns:

: A Axes object if ax is None else None.

Examples

import scanpy as sc
adata = sc.datasets.pbmc68k_reduced()
sc.pl.violin(adata, keys='S_score')
../_images/scanpy-pl-violin-1.png

Plot by category. Rotate x-axis labels so that they do not overlap.

sc.pl.violin(adata, keys='S_score', groupby='bulk_labels', rotation=90)
../_images/scanpy-pl-violin-2.png

Set order of categories to be plotted or select specific categories to be plotted.

groupby_order = ['CD34+', 'CD19+ B']
sc.pl.violin(adata, keys='S_score', groupby='bulk_labels', rotation=90,
    order=groupby_order)
../_images/scanpy-pl-violin-3.png

Plot multiple keys.

sc.pl.violin(adata, keys=['S_score', 'G2M_score'], groupby='bulk_labels',
    rotation=90)
../_images/scanpy-pl-violin-4.png

For large datasets consider omitting the overlaid scatter plot.

sc.pl.violin(adata, keys='S_score', stripplot=False)
../_images/scanpy-pl-violin-5.png