Skip to content

Allow using arbitrary vectors in plotting (not only obs columns) #1500

Description

@picciama
  • Additional function parameters / changed functionality / changed defaults?
  • New analysis tool: A simple analysis tool you have been using and are missing in sc.tools?
  • New plotting function: A kind of plot you would like to seein sc.pl?
  • External tools: Do you know an existing package that should go into sc.external.*?
  • Other?

Quite often I need to color UMAPs based on features that are not part of adata.X but adata.obsm for the reason that they are special. E.g. KO data with gRNAs versus endogenes/ target genes, or viral genes versus edogenes.

Example use case:

  • Cluster cells based on endogenes
  • UMAP and color by a bunch of viral genes

Clustering must not include these viral genes -> must be excluded from X.
I don't want to store so many additional columns in obs and I need to have these features separated in their own matrix for downstream analysis, which is why I want to use obsm.

Can we have sth. like this:

sc.pl.umap(adata, color='viral_genes')  # adata.obsm['viral_genes'] is a pandas.DataFrame ?

It shouldn't be overcomplicated I think, since this only involves an additional check: if the elements in the color arg list are not found in obs.columns nor var.columns, then check the keys in obsm and use the entire dataframe behind this key.

Activity

  1. ivirshup commented on Nov 19, 2020

    @ivirshup
    Member

    This has been worked on here: scverse/anndata#342

    The idea is to allow any vector from the anndata object to be used for coloring, but that PR seems a bit stalled at the moment. This would also be useful for providing parameters in other places, like regress_out.

  2. added this to the 1.8.0 milestone on Feb 10, 2021
  3. modified the milestones: 1.8.0, 1.9.0 on May 26, 2021
  4. modified the milestones: 1.9.0, 1.10.0 on Mar 21, 2022
  5. modified the milestones: 1.10.0, 1.11.0 on Feb 20, 2024
  6. modified the milestones: 1.11.0, 1.12.0 on Dec 20, 2024
  7. changed the title [-]Support obsm key to color UMAP[/-] [+]Allow using several `pp`, `tl`, and `pl` functions to operate on obsm representations[/+] on Oct 28, 2025
  8. changed the title [-]Allow using several `pp`, `tl`, and `pl` functions to operate on obsm representations[/-] [+]Allow using arbitrary vectors in plotting (not only `obs` columns)[/+] on Oct 31, 2025
  9. flying-sheep commented on Oct 31, 2025

    @flying-sheep
    Member

    I’ll finally tackle this soon! scverse/anndata#2189

  10. modified the milestones: 1.12.0, 1.13.0 on Nov 3, 2025
  11. patcon commented on Jan 13, 2026

    @patcon
    Contributor

    This is neat! But is there not a smaller scanpy-specific fix that might cover 99% of the use-cases people might have, without the deeper infrastructure overhead that seems to be quite onerous? (using obsm feels quite obvious and useful to me, though all the extra accessors seem interesting)

    In some of my own custom commands, I parse list-like strings (X_pca[:], X_pca[:10] or X_pca[2:6]) as references to obsm vectors that are expanded (e.g., ["X_pca[2]", "X_pca[3]", "X_pca[4]", "X_pca[5]"]. This is a bit like @picciama's suggestion, but doesn't opportunistically expand a string into looking in obsm (e.g., viral_genes) and auto-expanding in potentially unexpected ways into a list of column-wise vectors (e.g., ["viral_genes[0]", "viral_genes[1]", "viral_genes[2]", ...] which I read as implied by his message).

  12. flying-sheep commented on Jan 13, 2026

    @flying-sheep
    Member

    We’re open to PRs that add that stuff, as long as they are well-designed and there is no guessing!

    I think one of the early design mistakes of scanpy is “search this vector in multiple parts of the AnnData object, and if it’s in multiple, don’t give any way to select which one is meant”.

  13. moved this to meta/ backlog in scanpy 2.0on Feb 12, 2026
  14. modified the milestones: 1.13.0, 1.14.0 on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

  • Status
    meta/ backlog

Relationships

None yet

Development

No branches or pull requests

Issue actions