Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scanpy.pl.stacked_violin - remove "marker label" whitespace if var_names is a list of genes #3320

Open
3 tasks done
adkinsrs opened this issue Oct 24, 2024 · 6 comments
Open
3 tasks done

Comments

@adkinsrs
Copy link

adkinsrs commented Oct 24, 2024

Please make sure these conditions are met

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of scanpy.
  • (optional) I have confirmed this bug exists on the main branch of scanpy.

What happened?

Basically I am creating a stacked violin plot that uses a list of marker genes for the "var_names" argument. But whenever I create the plot, it has extra whitespace at the top of the plot where the marker gene labels should go. This is very evident when you add a figure title, which gets place above the padding whitespace. I have not really found a way around this, and am currently looking through the scanpy internal code to see if there is some padding setting that I can undo. If there is a workaround to this or some option that I am missing I would like to know.

Thanks!

Minimal code sample

# I have an AnnData object that has undergone the Seurat analysis. I named the Leiden clustering output "spatial_clusters" since I was testing a spatial dataset read in via spatialdata then converted to AnnData with "spatialdata_io.experimental.to_legacy_anndata"

marker_genes = ["Pou4f3", "Calb2", "Pvalb", "Smpx", "Mlf1", "Sox2"]  # 5 random Cochlear HCs P7 + Sox2

sc.pl.stacked_violin(adata, marker_genes, title="Marker gene expression per cluster", groupby="spatial_clusters", cmap="YlOrRd", show=False, return_fig=True)

### COMPARISON TO MARKER GENES WITH LABELS
marker_genes = {"IHC": ["Pou4f3", "Calb2", "Pvalb", "Smpx", "Mlf1"], "Random": ["Sox2"]}

sc.pl.stacked_violin(adata, marker_genes, title="Marker gene expression per cluster", groupby="spatial_clusters", cmap="YlOrRd", show=False, return_fig=True)

Error output

Will post in the next comment on this thread. Seems I cannot drag-n-drop images into this block.

Versions

I am including relevant package versions. Can provide more if needed

Python 3.12.7

anndata==0.10.6
matplotlib==3.9.0
pandas==2.2.1
scanpy-1.10.3
@adkinsrs adkinsrs added Bug 🐛 Triage 🩺 This issue needs to be triaged by a maintainer labels Oct 24, 2024
@adkinsrs
Copy link
Author

Error Output

Output when using a list of marker genes in the var_names arg
Image

Output when using a dict of marker genes in the var_names arg
Image

@flying-sheep flying-sheep added Area - Plotting 🌺 and removed Triage 🩺 This issue needs to be triaged by a maintainer labels Oct 25, 2024
@flying-sheep
Copy link
Member

Yeah, that’s true.

I think depending on what’s happening, it’s actually the spacer we add, not the area for the dendrogram, but sometimes we also need that spacer …

Our plotting code is complicated and needs to be overhauled. If anyone feels like diving int

@adkinsrs
Copy link
Author

adkinsrs commented Oct 25, 2024

I was able to "hack" my way to a solution. However I needed to call StackedViolin.make_figure to actually generate the figure that I could remove Axes from, and this creates the issue of showing the plot twice (one incorrect, one correct) in a Jupyter notebook. In a script though this shouldn't be an issue though.

marker_genes = ["Pou4f3", "Calb2", "Pvalb", "Smpx", "Mlf1", "Sox2"]  # 5 random Cochlear HCs P7 + Sox2

violin_fig = sc.pl.stacked_violin(vis_adata, marker_genes, var_group_positions=None, title="Marker gene expression per cluster", groupby="spatial_clusters", cmap="YlOrRd", show=False, return_fig=True)

# Remove the existing legend and add a new vertically-oriented one
violin_fig.legend(show = False)
violin_fig.add_totals()
violin_fig.make_figure()

# For some reason, deleting all axes and remaking the figure makes it without the spacer above the plot (which was in ax[2] I think)
violin_axes = violin_fig.fig.get_axes()

for ax in violin_axes:
    violin_fig.fig.delaxes(ax)
violin_fig.make_figure()

Image

@flying-sheep
Copy link
Member

very cool! Happy you found a workaround. I’d really like to toss a lot of the plotting out and replace it with something declarative …

@mxposed
Copy link
Contributor

mxposed commented Nov 1, 2024

Our plotting code is complicated and needs to be overhauled. If anyone feels like diving int

@flying-sheep I feel like diving in, do you have an issue/PR to work on? I've been using scanpy for a long time, and want to contribute

@adkinsrs
Copy link
Author

adkinsrs commented Nov 1, 2024

Hi @mxposed,

I also see the same thing with dotplots as well. My workaround is the same as with the stacked_violin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants