spatialdata-plot
spatialdata-plot copied to clipboard
modify colormap so that 0 values have alpha=0
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
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?
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
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?
hello, @giovp @timtreis . I used the same code according to the tutorial, but the result is not ideal, is it the version reason?
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!
Thanks for reporting, I will answer with the solution in the linked issue.
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.
@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)?
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?
maybe in a utils function then? otherwise the code to do it gets lost in github issues
yeah maybe a tiny utils would be helpful