spatialdata-plot
spatialdata-plot copied to clipboard
Different epression plot from different get_cmap()
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.
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))