This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to adjust y-axis scale on scanpy scatter plot

Is there away to adjust y-axis scale of scanpy plots e.g. scatter and others https://scanpy.readthedocs.io/en/stable/generated/scanpy.pl.scatter.html in their documentation it seems like I can't adjust the axis unless it's single component

This is what I'm trying to plot sc.pl.scatter(adata, x = 'total_counts', y='pct_counts_mt') I attempted to do sc.pl.scatter(adata, x = 'total_counts', y='pct_counts_mt', ylim=(0,50))

returns error TypeError: scatter() got an unexpected keyword argument 'ylim' Is there another way to adjust the y axis scale

scrna seq scanpy data rna

your answer is the most correct one. Worship

1 answer

Suggest you should try:

ax = sc.pl.scatter(adata, x = 'total_counts', y='pct_counts_mt',show=False)
ax.set_ylim(0,50);

Explanation

The key that I found to making this work is based on a note in Workhorse's comment under here in which they noted you needed the setting return_fig=True for sc.pl.dotplot(). Looking at the scanpy.pl.scatter documentation, it seems the equivalent is the show setting.

"Show the plot, do not return axis."

I had been seeing the sc.pl.scatter() based on something like the OP had was returning nothing. Adding in show=False will get a matplotlib axes object (matplotlib.axes._axes.Axes) returned that can then be adjusted using ax.set_ylim(), similar to ivirshup's suggestion here. (ivirshup had show=False in use there; however, I didn't pick up on that until seeing Workhorse's comment and what is returned by scanpy.pl.scatter isn't iterable so that the use of for ax... or with ax... doesn't work, like apparently for ax... works [or worked?] for sc.pl.rank_genes_groups_violin().)

Applying that to your case, I suggest you should use:

ax = sc.pl.scatter(adata, x = 'total_counts', y='pct_counts_mt',show=False)
ax.set_ylim(0,50);

(You can leave off the semi-colon if the ax.set_ylim(0,50) won't be the last line in a Jupyter cell.)

That suggestion likely working is supported by testing in Jupyter sessions provided by the MyBInder service launched from here where the steps to install scanpy under here were followed and then the first couple of code cells in the included 'analysis-visualization-spatial.ipynb' run to define an annotated data matrix adata, before using this code to test the plotting of a scatter plot like OP was using:

ax = sc.pl.scatter(adata, x = 'total_counts', y='pct_counts_in_top_50_genes',show=False)
ax.set_ylim(0,50);

Thank you for the detailed explanation, it worked perfectly!

Log in to answer this question.