Skip to content

Cannot be shown in transparent colors when Plotting the gene expression data #554

Closed

Description

@Bioin-Mixologist

When I reproduce according to the code of the official tutorial Technology focus: Visium HD in jupyterlab, I get an incorrect figure: transparent colors RGBA(1.0,1.0,1.0,0.0) is replaced by white RGB(1.0,1.0,1.0).
How to modify the code?

fact vs expect

Associate packages

spatialdata               0.1.2                    pypi_0    pypi
spatialdata-io            0.1.2                    pypi_0    pypi
spatialdata-plot          0.2.1                    pypi_0    pypi
napari-spatialdata        0.4.1                    pypi_0    pypi
matplotlib                3.8.4                    pypi_0    pypi
matplotlib-inline         0.1.7                    pypi_0    pypi
matplotlib-scalebar       0.8.1                    pypi_0    pypi
napari-matplotlib         2.0.1                    pypi_0    pypi
ipympl                    0.9.4                    pypi_0    pypi
numpy                     1.23.4                   pypi_0    pypi

The Plot Code:

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)
/tmp/ipykernel_2057419/2380312474.py:8:MatplotlibDeprecationWarning:The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ''matplotlib.colormaps[name]'' or ''matplotlib.colormaps.get_cmap(obj)'' instead.
viridis= cm.get cmap("viridis",256)
gene_name = "AA986860"

# 结合H&E切片绘制16um,8um级
for bin_size in [16, 8]:
    sdata_small.pl.render_images("Visium_HD_Mouse_Small_Intestine_full_image").pl.render_shapes(
        f"Visium_HD_Mouse_Small_Intestine_square_{bin_size:03}um", color=gene_name, cmap=new_cmap
    ).pl.show(coordinate_systems="global", title=f"bin_size={bin_size}µm", figsize=(10, 10))
clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]for integers)
clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]for integers)

The plot output:

incorrect
3a168195a49eb39a7bc503b2bff3baba

Activity

  1. Bioin-Mixologist commented on Apr 30, 2024

    @Bioin-Mixologist
    Author

    The data from:

    wget -c https://s3.embl.de/spatialdata/spatialdata-sandbox/visium_hd_3.0.0_io.zip &

    To make it easier for others to reproduce this issue, I've simplified the code. (circle not square )

    import spatialdata as sd
    visium_hd_zarr_path = "/Path/to/visium_hd_Mu_intestin/data.zarr"
    
    # input data
    sdata = sd.read_zarr(visium_hd_zarr_path)
    sdata
    
    # make the var names unique
    for table in sdata.tables.values():
        table.var_names_make_unique()
    import matplotlib.pyplot as plt
    import spatialdata_plot
    from spatialdata import get_extent
    from spatialdata import bounding_box_query
    
    data_extent = get_extent(sdata["Visium_HD_Mouse_Small_Intestine_full_image"], coordinate_system="global")
    data_extent
    
    queried_cytassist = bounding_box_query(
        sdata["Visium_HD_Mouse_Small_Intestine_cytassist_image"],
        min_coordinate=[data_extent["x"][0], data_extent["y"][0]],
        max_coordinate=[data_extent["x"][1], data_extent["y"][1]],
        axes=("x", "y"),
        target_coordinate_system="global",
    )
    sdata["queried_cytassist"] = queried_cytassist
    sdata_small = sdata.query.bounding_box(
        min_coordinate=[7000, 11000], max_coordinate=[10000, 14000], axes=("x", "y"), target_coordinate_system="global"
    )
    import matplotlib as mpl
    #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 = mpl.colormaps.get_cmap("viridis")
    # 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)
    plt.get_cmap(new_cmap)
    
    gene_name = "AA986860"
    for bin_size in [16, 8, 2]:
        sdata_small.pl.render_images("Visium_HD_Mouse_Small_Intestine_full_image").pl.render_shapes(
            f"Visium_HD_Mouse_Small_Intestine_square_{bin_size:03}um", color=gene_name, cmap=new_cmap
        ).pl.show(coordinate_systems="global", title=f"bin_size={bin_size}µm", figsize=(10, 10))

    The output is likely.
    I thought of two possible reasons: 1) The matplotlib section sets the color bar code incorrectly; 2) Errors in gene expression value conversion.

    16umGeneExp

  2. Bioin-Mixologist commented on May 2, 2024

    @Bioin-Mixologist
    Author

    The author has already answered in spatialdata-plot issues 242 and release spatialdata-plot 0.2.2, spatialdata-plot pull 239.
    The transparent code1.0 from spatialdata-plot issues 242.
    after upgrade spatialdata-plot 0.2.2, the figure is ok!

    pip install --upgrade spatialdata-plot==0.2.2

    upgrade

    the get_cmap() question is new issue spatialdata-plot issues 255

  3. Bioin-Mixologist commented on May 2, 2024

    @Bioin-Mixologist
    Author

    Yeh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions