Skip to content

modify colormap so that 0 values have alpha=0 #242

Closed

Description

@giovp

saw this from @LucaMarconato tutorial on visium HD scverse/spatialdata-tutorials#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?

Activity

  1. grst commented on Apr 9, 2024

    @grst
    Contributor

    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
  2. timtreis commented on Apr 11, 2024

    @timtreis
    Member

    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?

  3. Bioin-Mixologist commented on May 2, 2024

    @Bioin-Mixologist

    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!

  4. LucaMarconato commented on May 2, 2024

    @LucaMarconato
    Member

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

  5. Bioin-Mixologist commented on May 2, 2024

    @Bioin-Mixologist

    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

  6. LucaMarconato commented on Jul 11, 2024

    @LucaMarconato
    Member

    @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)?

  7. timtreis commented on Jul 11, 2024

    @timtreis
    Member

    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?

  8. giovp commented on Jul 12, 2024

    @giovp
    MemberAuthor

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

  9. LucaMarconato commented on Jul 12, 2024

    @LucaMarconato
    Member

    yeah maybe a tiny utils would be helpful

  10. self-assigned this
    on Jul 12, 2024
  11. linked a pull request that will close this issueAdded utils function for 0-transparent cmaps #302on Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions