labsyspharm / scimap

Spatial Single-Cell Analysis Toolkit
https://scimap.xyz/
MIT License
74 stars 26 forks source link

Adjusting colors in scimap UMAP animation #31

Open jnmark opened 2 years ago

jnmark commented 2 years ago

Hi, first of all, great package! Have been using the animate function pretty extensively. :)

I had a question about adjusting colors in sm.hl.animate - I would like to use the cluster colors stored in adata.uns['cluster_colors'] for the UMAP and spatial plot. I tried a few things - creating a colormap from the numerical values of the color array and specifying this with the cmap argument, zipping the colors into a dictionary and providing it in the palette argument, and then directly mapping the categories to colors in the function itself using palette = {0: '#1f77b4', 1: '#ff7f0e'...}, but none of these options seem to change the colors. It would be very helpful to have the colors retained in the animation since the rest of my analysis is color coded according to the cluster colors in adata.uns. Thanks for the help!

ajitjohnson commented 2 years ago

Hi @jnmark before I embark on trying to figure this out, have you tried palette = {'0': '#1f77b4', '1': '#ff7f0e'...} passing your cluster names as a string such as '0' and '1' ? Thank you.

jnmark commented 2 years ago

Hi @ajitjohnson thanks for catching my rookie error. That worked perfectly, yes! :) While we are on the topic of enhancement, could I also ask how the cluster order is decided in the legend? It doesn't seem to be sequential.

ajitjohnson commented 2 years ago

@jnmark I think it is the default matplotlib order (Is it likely alphabetical ordering or numeric ordering. like 0,1,10,...,19, 2, 20, ...).

If you want a specific ordering I would try to re-categorize and sort your column.

anticipated_order = ['A', 'C', 'B', .....]
adata.obs['column_that_is_colored'] = adata.obs['column_that_is_colored'].astype('category')
adata.obs['column_that_is_colored'] = adata.obs['column_that_is_colored'].cat.reorder_categories(anticipated_order )
adata.obs = adata.obs.sort_values('column_that_is_colored')