|
10 | 10 | from spatialdata.models import Image2DModel |
11 | 11 |
|
12 | 12 | import spatialdata_plot # noqa: F401 |
| 13 | +from spatialdata_plot._logging import logger, logger_warns |
13 | 14 | from spatialdata_plot.pl.render import _is_rgb_image |
14 | 15 | from tests.conftest import DPI, PlotTester, PlotTesterMeta, _viridis_with_under_over |
15 | 16 |
|
@@ -545,13 +546,49 @@ def test_plot_channels_as_legend_legend_lower_right(self, sdata_blobs: SpatialDa |
545 | 546 | legend_loc="lower right" |
546 | 547 | ) |
547 | 548 |
|
| 549 | + def test_plot_channels_as_legend_single_channel(self, sdata_blobs: SpatialData): |
| 550 | + sdata_blobs.pl.render_images(element="blobs_image", channel=0, channels_as_legend=True).pl.show() |
| 551 | + |
| 552 | + def test_plot_channels_as_legend_sequential_single_channels(self, sdata_blobs_str: SpatialData): |
| 553 | + ( |
| 554 | + sdata_blobs_str.pl.render_images( |
| 555 | + element="blobs_image", |
| 556 | + channel="c1", |
| 557 | + palette=["cyan"], |
| 558 | + alpha=0.5, |
| 559 | + channels_as_legend=True, |
| 560 | + ) |
| 561 | + .pl.render_images( |
| 562 | + element="blobs_image", |
| 563 | + channel="c2", |
| 564 | + palette=["magenta"], |
| 565 | + alpha=0.5, |
| 566 | + channels_as_legend=True, |
| 567 | + ) |
| 568 | + .pl.show() |
| 569 | + ) |
| 570 | + |
548 | 571 |
|
549 | 572 | class TestChannelsAsCategoriesNonVisual: |
550 | 573 | """Non-visual tests for channels_as_legend edge cases.""" |
551 | 574 |
|
552 | | - def test_channels_as_legend_ignored_for_single_channel(self, sdata_blobs: SpatialData): |
| 575 | + def test_channels_as_legend_single_channel_shows_legend_no_colorbar(self, sdata_blobs: SpatialData): |
553 | 576 | fig, ax = plt.subplots() |
554 | 577 | sdata_blobs.pl.render_images(element="blobs_image", channel=0, channels_as_legend=True).pl.show(ax=ax) |
| 578 | + legend = ax.get_legend() |
| 579 | + assert legend is not None |
| 580 | + assert "0" in [t.get_text() for t in legend.get_texts()] |
| 581 | + assert len(fig.axes) == 1 # no colorbar inset axes |
| 582 | + plt.close("all") |
| 583 | + |
| 584 | + def test_channels_as_legend_rgb_warns_and_no_legend(self, caplog): |
| 585 | + data = np.zeros((3, 50, 50), dtype=np.float64) |
| 586 | + data[0], data[1], data[2] = 0.8, 0.2, 0.1 |
| 587 | + img = Image2DModel.parse(data, dims=("c", "y", "x"), c_coords=["r", "g", "b"]) |
| 588 | + sdata = SpatialData(images={"img": img}) |
| 589 | + fig, ax = plt.subplots() |
| 590 | + with logger_warns(caplog, logger, match="not supported for true RGB"): |
| 591 | + sdata.pl.render_images("img", channels_as_legend=True).pl.show(ax=ax) |
555 | 592 | assert ax.get_legend() is None |
556 | 593 | plt.close("all") |
557 | 594 |
|
|
0 commit comments