How to adjust y-axis scale on scanpy scatter plot
1
0
Entering edit mode
20 months ago
Emily ▴ 70

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 • 2.5k views
ADD COMMENT
2
Entering edit mode
20 months ago
Wayne ★ 1.9k

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);
ADD COMMENT
1
Entering edit mode

Thank you for the detailed explanation, it worked perfectly!

ADD REPLY

Login before adding your answer.

Traffic: 2195 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6