diff --git a/src/spatialdata_plot/pl/utils.py b/src/spatialdata_plot/pl/utils.py index 60e84c1e..20bb111e 100644 --- a/src/spatialdata_plot/pl/utils.py +++ b/src/spatialdata_plot/pl/utils.py @@ -344,7 +344,12 @@ def _prepare_cmap_norm( **kwargs: Any, ) -> CmapParams: is_default = cmap is None - cmap = copy(matplotlib.colormaps[rcParams["image.cmap"] if cmap is None else cmap]) + if cmap is None: + cmap = rcParams["image.cmap"] + if isinstance(cmap, str): + cmap = matplotlib.colormaps[cmap] + + cmap = copy(cmap) cmap.set_bad("lightgray" if na_color is None else na_color) if isinstance(norm, Normalize) or not norm: diff --git a/tests/_images/Images_can_pass_cmap_to_render_images.png b/tests/_images/Images_can_pass_cmap_name_to_render_images.png similarity index 100% rename from tests/_images/Images_can_pass_cmap_to_render_images.png rename to tests/_images/Images_can_pass_cmap_name_to_render_images.png diff --git a/tests/_images/Images_can_pass_cmap_object_to_render_images.png b/tests/_images/Images_can_pass_cmap_object_to_render_images.png new file mode 100644 index 00000000..f18fa5f0 Binary files /dev/null and b/tests/_images/Images_can_pass_cmap_object_to_render_images.png differ diff --git a/tests/pl/test_render_images.py b/tests/pl/test_render_images.py index a1b732ff..338504b1 100644 --- a/tests/pl/test_render_images.py +++ b/tests/pl/test_render_images.py @@ -24,9 +24,12 @@ class TestImages(PlotTester, metaclass=PlotTesterMeta): def test_plot_can_render_image(self, sdata_blobs: SpatialData): sdata_blobs.pl.render_images(elements="blobs_image").pl.show() - def test_plot_can_pass_cmap_to_render_images(self, sdata_blobs: SpatialData): + def test_plot_can_pass_cmap_name_to_render_images(self, sdata_blobs: SpatialData): sdata_blobs.pl.render_images(elements="blobs_image", cmap="seismic").pl.show() + def test_plot_can_pass_cmap_object_to_render_images(self, sdata_blobs: SpatialData): + sdata_blobs.pl.render_images(elements="blobs_image", cmap=matplotlib.colormaps["seismic"]).pl.show() + def test_plot_can_render_a_single_channel_from_image(self, sdata_blobs: SpatialData): sdata_blobs.pl.render_images(elements="blobs_image", channel=0).pl.show()