Some of my samples have empty clusters. I want to keep the same color for each cluster in different plots.

How do I get sc.pl.umap to keep the same colors for each cluster, even when a cluster is empty?

from matplotlib.pyplot import rc_context
with rc_context({'figure.figsize': (3, 3)}):
    fig, (ax1, ax2) = plt.subplots(2, 2, figsize=(6,6))
    ax1[0] = sc.pl.umap(adata[adata.obs['sample'] == adata.obs['sample'].cat.categories[0]], 
                 color = 'leiden_r1', size=20,
                 legend_loc = 'on data', ax=ax1[0],
                 palette = mycolormap_26,
                 frameon=True, legend_fontsize = 8,title = adata.obs['sample'].cat.categories[0], show=False)
    ax1[0].set(xlim=xlim, ylim=ylim)
    ax1[1] = sc.pl.umap(adata[adata.obs['sample'] == adata.obs['sample'].cat.categories[1]], 
                 color = 'leiden_r1', size=20,
                 legend_loc = 'on data', ax=ax1[1],
                 palette = mycolormap_26,
                 frameon=True, legend_fontsize = 8,title = adata.obs['sample'].cat.categories[1], show=False)
    ax1[1].set(xlim=xlim, ylim=ylim)
    ax2[0] = sc.pl.umap(adata[adata.obs['sample'] == adata.obs['sample'].cat.categories[2]], 
                 color = 'leiden_r1', size=20,
                 legend_loc = 'on data', ax=ax2[0],
                 palette = mycolormap_26,
                 frameon=True, legend_fontsize = 8,title = adata.obs['sample'].cat.categories[2], show=False)
    ax2[0].set(xlim=xlim, ylim=ylim)
    ax2[1] = sc.pl.umap(adata[adata.obs['sample'] == adata.obs['sample'].cat.categories[3]], 
                 color = 'leiden_r1', size=20,
                 legend_loc = 'on data', ax=ax2[1],
                 palette = mycolormap_26,
                 frameon=True, legend_fontsize = 8,title = adata.obs['sample'].cat.categories[3], show=False)
    ax2[1].set(xlim=xlim, ylim=ylim)

image Thanks

  • 1
    $\begingroup$ Good question. I assume there's a import scanpy as sc . What about inserting a dumbie value, say single 0,0 to prevent the skip? It appears UMAP1 cluster 1 is absent causing 2 and 3 ... thereon to change colour (colour) schemes for the proceeding colour (colour) $\endgroup$
    – M__
    Dec 29, 2022 at 22:27
  • 1
    $\begingroup$ I can customize the color palette mycolormap_26 for each specific sample, which is tedious. $\endgroup$
    – Dan Li
    Jan 1 at 22:30
  • $\begingroup$ This could be scripted, which would remove the tedium, so it would only need to be done once. What you've hit is a shortcoming in ScanPy and could be reported (answer below). $\endgroup$
    – M__
    Jan 2 at 12:48

1 Answer 1


My recommendation is to report the issue directly on the ScanPy Github page here: https://github.com/scverse/scanpy/issues

This is a shortcoming with the testing of the algorithm, without any question. Replicates with experimental modification are standard procedure in this line of analysis. For example, one in vitro cell culture does not receive the drug therapy, i.e. a control, and the rest do, which results in one group with a complete absence of a transcript profile. The authors simply have not described this possibility in any of their documentation and therefore may not have originally considered it.

What I suspect has happened is the authorship only tested extensive and complete data - like really big data sets. "BigData" is what ScanPy is targeting because they look to have adapted to large data in Wolf et al Genome Biology 2018 paper, notably by specifying how quick their algorithm performs with increasing data set size.

What I suspect has happened is that smaller data sets, with experimental replication and modification, were not tested so these issues simply get missed. It's easy to overlook, the authors can't think of everything.

The authors of ScanPy are responding to issues arising unevenly. So it will be hit and miss whether they respond. Similar issues should have been reported to them before because its a common wet-lab experimental design. However, I've not checked their full intray.

They should provide the bit of code that will prevent the tedium described above, but then again they may not. If they do, please do write back. The whole ScanPy project is interesting.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.