How to get dot values from `sc.pl.dotplot()`?

Hello,
I’m using the sc.pl.dotplot function to draw dotplots, and I’m interested to get the fraction of cells and mean expression values for each dot.
If I assign the function call output to a variable dp, I can see some values related the axes, but is there a wat to get the values the dots?

I’m using this:

import scanpy as sc
adata = sc.datasets.pbmc68k_reduced()
markers = ['C1QA', 'PSAP', 'CD79A', 'CD79B', 'CST3', 'LYZ']
dp=sc.pl.dotplot(adata, markers, groupby="bulk_labels", return_fig=True)
dp
# <scanpy.plotting._dotplot.DotPlot at 0x7fd840a03df0>

dp.get_axes()
# {'mainplot_ax': <Axes: >,
# 'size_legend_ax': <Axes: title={'center': 'Fraction of cells\nin group (%)'}>,
# 'color_legend_ax': <Axes: title={'center': 'Mean expression\nin group'}>}

There isn’t a direct way, but using the recent release candidate for scanpy you could use the new function sc.get.aggregate to accomplish this:

Something like:

aggregated = sc.get.aggregate(
    adata[:, markers],
    by="bulk_labels",
    func=["sum", "count_nonzero"],
)
mean_in_expressed = aggregated.to_df(layer="sum") / aggregated.to_df(layer="count_nonzero")
1 Like