Skip to content

Commit 160f781

Browse files
committed
Suppress colorbar when channels_as_legend=True; fix sequential test opacity; add colorbar suppression test
1 parent 8e0c2dc commit 160f781

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/spatialdata_plot/pl/render.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,12 @@ def _render_images(
13881388
is_continuous=True,
13891389
auto_condition=n_channels == 1,
13901390
)
1391-
if wants_colorbar and legend_params.colorbar and colorbar_requests is not None:
1391+
if (
1392+
wants_colorbar
1393+
and legend_params.colorbar
1394+
and colorbar_requests is not None
1395+
and not render_params.channels_as_legend
1396+
):
13921397
sm = plt.cm.ScalarMappable(cmap=cmap, norm=render_params.cmap_params.norm)
13931398
colorbar_requests.append(
13941399
ColorbarSpec(
Binary file not shown.
Binary file not shown.

tests/pl/test_render_images.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,14 @@ def test_plot_channels_as_legend_sequential_single_channels(self, sdata_blobs_st
555555
element="blobs_image",
556556
channel="c1",
557557
palette=["cyan"],
558+
alpha=0.5,
558559
channels_as_legend=True,
559560
)
560561
.pl.render_images(
561562
element="blobs_image",
562563
channel="c2",
563564
palette=["magenta"],
565+
alpha=0.5,
564566
channels_as_legend=True,
565567
)
566568
.pl.show()
@@ -578,6 +580,13 @@ def test_channels_as_legend_single_channel_shows_legend(self, sdata_blobs: Spati
578580
assert "0" in [t.get_text() for t in legend.get_texts()]
579581
plt.close("all")
580582

583+
def test_channels_as_legend_single_channel_suppresses_colorbar(self, sdata_blobs: SpatialData):
584+
fig, ax = plt.subplots()
585+
sdata_blobs.pl.render_images(element="blobs_image", channel=0, channels_as_legend=True).pl.show(ax=ax)
586+
# channels_as_legend replaces the colorbar — only the legend axes should exist
587+
assert len(fig.axes) == 1
588+
plt.close("all")
589+
581590
def test_channels_as_legend_rgb_warns_and_no_legend(self, caplog):
582591
data = np.zeros((3, 50, 50), dtype=np.float64)
583592
data[0], data[1], data[2] = 0.8, 0.2, 0.1

0 commit comments

Comments
 (0)