.ann_hist
- proteopy.pl.ann_hist(adata, x=None, y=None, level_key=None, axis=None, zero_to_na=False, fill_na=None, vline=None, hline=None, show=True, save=None, **kwargs)[source]
Plot a histogram of
.obsor.varannotation columns.Thin wrapper around
seaborn.histplot(). The annotation columns are collected fromadata.obsoradata.var, optionally reduced to one row perlevel_keygroup, and passed tosns.histplottogether with all extra keyword arguments.- Parameters:
adata (AnnData) –
AnnDataholding the annotations.x (str | None) – Annotation column plotted on the x axis. Same meaning as
xinseaborn.histplot(), except that it names a.obs/.varcolumn.y (str | None) – Annotation column plotted on the y axis. Same meaning as
yinseaborn.histplot(). Pass onlyyfor a horizontal histogram, or bothxandyfor a bivariate histogram. At least one ofxandyis required.level_key (str | list[str] | None) – Column(s) defining the level the annotation belongs to. Annotations often describe a higher level than the axis and are repeated for every row of that level, e.g.
protein_q_valueis repeated for all peptides of a protein in a peptide-level AnnData. Withlevel_key="protein_id", each protein contributes one value instead of one per peptide.axis ({"obs", "var"} | None) – Annotation axis holding the columns. If
None, it is inferred from where the columns live.zero_to_na (bool) – If True, replace zeros with NaN in the numeric
x/ycolumns before plotting.fill_na (float | int | str | None) – If not None, replace NaN in the
x/ycolumns with this value, e.g."missing"for str or categorical annotations.vline (dict | list[dict] | None) – Vertical line(s) drawn with
matplotlib.axes.Axes.axvline(). A dict defines one line, a list of dicts several. Keys:value(required, numeric x position),color(default"red"),linestyle(default"dashed") andtext(defaultNone). Lines withtextare listed in a legend to the right of the plot. On a categorical axis, categoryisits at positioni.hline (dict | list[dict] | None) – Horizontal line(s) drawn with
matplotlib.axes.Axes.axhline(). Same format asvline, withvalueas the y position.show (bool) – Call
matplotlib.pyplot.show()at the end.save (str | Path | None) – Save the figure to this path. If None, do not save.
**kwargs – Passed verbatim to
seaborn.histplot()(e.g.bins,binwidth,stat,discrete,shrink,element,kde,log_scale,hue,multiple,palette). See https://seaborn.pydata.org/generated/seaborn.histplot.html for the full list and what each argument does.hueandweightsgiven as strings name.obs/.varcolumns from the same frame asx/y.dataandaxare reserved.
- Returns:
The Axes the histogram was drawn on.
- Return type:
Axes
- Raises:
KeyError – If a referenced column is missing.
ValueError – If neither
xnoryis given,axisisNoneand a plotted column exists in both.obsand.var, the plotted columns come from different annotation axes, a plotted column is not constant within alevel_keygroup, or avline/hlinedict has unknown keys or no finite numericvalue.TypeError – If a reserved keyword is passed, a column has an unsupported dtype (e.g. bool or datetime), or
vline/hlineis not a dict or a list of dicts.
Notes
Non-numeric values are ordered by category order for categorical columns, otherwise lexicographically after
strcoercion. To impose a custom order, store the column as an ordered Categorical:>>> adata.var["fraction"] = pd.Categorical( ... adata.var["fraction"], ... categories=["F1", "F2", "F10"], ... ordered=True, ... )
Examples
>>> import proteopy as pr >>> adata = pr.load.toy_simple_peptides( ... n_proteins=10, ... n_peptides_per_protein=( ... [5] * 1 + [6] * 2 + [7] * 5 + [8] * 2 ... ), ... ) >>> pr.pl.ann_hist( ... adata, ... x="Protein.Q.Value", ... level_key="protein_id", ... vline=[ ... {"value": 0.01, "text": "1% FDR"}, ... {"value": 0.05, "color": "grey", "text": "5% FDR"}, ... ], ... bins=50, ... ) >>> pr.pl.ann_hist( ... adata, ... x="Q.Value", ... y="Q.Value.Global", ... ) >>> pr.pl.ann_hist(adata, x="condition", shrink=0.8)