spatialdata-plot icon indicating copy to clipboard operation
spatialdata-plot copied to clipboard

modify colormap so that 0 values have alpha=0

Open giovp opened this issue 1 year ago • 5 comments

saw this from @LucaMarconato tutorial on visium HD https://github.com/scverse/spatialdata-notebooks/pull/93

import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# modify the viridis colormap, so that the top color is a green (better visible on the H&E pink), and such that
# the value 0 leads to a transparent color
viridis = cm.get_cmap("viridis", 256)
# using 0.8 instead of 1.0 truncates the colormap
colors = viridis(np.linspace(0, 0.8, 256))
# set the color of zero to be transparent
colors[0, :] = [1.0, 1.0, 1.0, 0.0]

new_cmap = LinearSegmentedColormap.from_list("truncated_viridis", colors)

it's actually really nice and result to this

image

I think it could just be an option to do this modification internally? with an argument like alpha_value_zero: bool = True or so, wdyt?

giovp avatar Apr 03 '24 12:04 giovp

Could also be a helper function that takes an arbitrary cmap and returns a modified one? Personally, I'm not a big fan of the plotting functions accumulating too many arguments.

I've been using something similar for visium data actually... this version makes a gradient from transparent to viridis:

def cmap_alpha(cmap="viridis_r"):
    cmap = cm.get_cmap(camp)
    colors = cmap(np.arange(cmap.N))
    colors[: int(cmap.N / 2), 3] = np.linspace(0, 1, int(cmap.N / 2))  # Modify alpha channel
    colors[int(cmap.N / 2) :, 3] = 1  # Modify alpha channel
    cmap = ListedColormap(colors)
    return cmap

grst avatar Apr 09 '24 12:04 grst

I do see the usecase but I'm not a fan of putting 0 as "missing", in this case I think it'd be cleaner to set those to NA and hide them through na_color. A hacky way would be to instead expose an option that modifies a given cmap so that the gradient towards 0 is not just towards white but transparent white?

timtreis avatar Apr 11 '24 17:04 timtreis

hello, @giovp @timtreis . I used the same code according to the tutorial, but the result is not ideal, is it the version reason? 326416012-af230d86-2599-4988-9285-00fa62ac1a53

I have open one issue Cannot be shown in transparent colors when Plotting the gene expression data #554 at spatialdata. There are also people who have recently had the same problem as me.

Thanks!

Bioin-Mixologist avatar May 02 '24 10:05 Bioin-Mixologist

Thanks for reporting, I will answer with the solution in the linked issue.

LucaMarconato avatar May 02 '24 11:05 LucaMarconato

Thankfully @LucaMarconato @timtreis @grst , after the update spatialdata-plot, the issue has been resolved and the issue Cannot be shown in transparent colors when Plotting the gene expression data #554 will be closed, but there may be new questions about this code, and a new issue #255 has been open.

upgrade newissue

Bioin-Mixologist avatar May 02 '24 14:05 Bioin-Mixologist

@timtreis is there a new recommended way to the achieve the transparent effect for zero values or shall we keep using the old code (i.e. manually modifying the cmap)?

LucaMarconato avatar Jul 11 '24 14:07 LucaMarconato

I'm not a big fan of the plotting functions accumulating too many arguments.

I'd really agree with Gregor here. This feels like a very specific thing that I don't necessarily think should be part of the function signature. I'd be in favor of casually showing such a function in one of the tutorials so that we can link to it, though? wdyt?

timtreis avatar Jul 11 '24 18:07 timtreis

maybe in a utils function then? otherwise the code to do it gets lost in github issues

giovp avatar Jul 12 '24 08:07 giovp

yeah maybe a tiny utils would be helpful

LucaMarconato avatar Jul 12 '24 14:07 LucaMarconato