Skip to content

Different epression plot from different get_cmap() #255

Closed

Description

@Bioin-Mixologist

From spatialdata issues 554 new issue.
Because matplotlib warning, use ''matplotlib.colormaps[name]'' or ''matplotlib.colormaps.get_cmap(obj)'' instead cm.get cmap("viridis",256), however, found the exp range are diferent.
newissue

code cm.get_cmap() and warning:

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)

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

so instead but plot exp only part.
code colormap.get_cmap():

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")
# Or
# viridis = mpl.colormaps["viridis"]

# using 0.8 instead of 1.0 truncates the colormap
colors = viridis(np.linspace(0, 0.8, num = 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))

Activity

  1. timtreis commented on Mar 29, 2026

    @timtreis
    Member

    Hi, thanks for reporting this. This is actually a matplotlib API difference, not a spatialdata-plot bug.

    cm.get_cmap("viridis", 256) returns a resampled ListedColormap, while mpl.colormaps.get_cmap("viridis") returns the original LinearSegmentedColormap. These behave differently when sampled with np.linspace. To get equivalent behavior with the new API, use resampled():

    import matplotlib as mpl
    
    # Old (deprecated):
    # viridis = cm.get_cmap("viridis", 256)
    
    # New (equivalent):
    viridis = mpl.colormaps["viridis"].resampled(256)
    

    Note that in matplotlib 3.10+ this discrepancy is resolved and both APIs produce identical results.
    Closing as this is upstream matplotlib behavior rather than a spatialdata-plot issue.

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