Skip to content

Commit 38416bb

Browse files
committed
constraining plot layout
1 parent 3a9b396 commit 38416bb

1 file changed

Lines changed: 66 additions & 29 deletions

File tree

tests/pl/test_colorbar.py

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import matplotlib
2+
import matplotlib.pyplot as plt
23
import numpy as np
34
import scanpy as sc
45
from spatialdata import SpatialData
@@ -28,53 +29,82 @@ def _make_multi_image_single_channel_sdata(self) -> SpatialData:
2829
return SpatialData(images={"img1": img1, "img2": img2}, labels={"lab": labels})
2930

3031
def test_plot_image_auto_colorbar_for_single_channel(self, sdata_blobs: SpatialData):
31-
sdata_blobs.pl.render_images("blobs_image", element="img").pl.show()
32+
fig, ax = plt.subplots()
33+
sdata_blobs.pl.render_images("blobs_image", element="img").pl.show(ax=ax)
34+
fig.tight_layout()
3235

3336
def test_plot_colorbar_img_default_location(self, sdata_blobs: SpatialData):
34-
(sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds").pl.show())
37+
fig, ax = plt.subplots()
38+
(sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds").pl.show(ax=ax))
39+
fig.tight_layout()
3540

3641
def test_plot_colorbar_img_bottom(self, sdata_blobs: SpatialData):
37-
(
38-
sdata_blobs.pl.render_images(
39-
"blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "bottom"}
40-
).pl.show()
42+
fig, ax = plt.subplots()
43+
sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "bottom"}).pl.show(
44+
ax=ax
4145
)
46+
fig.tight_layout()
4247

4348
def test_plot_colorbar_img_left(self, sdata_blobs: SpatialData):
44-
(sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "left"}).pl.show())
49+
fig, ax = plt.subplots()
50+
(
51+
sdata_blobs.pl.render_images(
52+
"blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "left"}
53+
).pl.show(ax=ax)
54+
)
55+
fig.tight_layout()
4556

4657
def test_plot_colorbar_img_top(self, sdata_blobs: SpatialData):
47-
(sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "top"}).pl.show())
58+
fig, ax = plt.subplots()
59+
(
60+
sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "top"}).pl.show(
61+
ax=ax
62+
)
63+
)
64+
fig.tight_layout()
4865

4966
def test_plot_colorbar_can_adjust_width(self, sdata_blobs: SpatialData):
50-
(sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"width": 0.4}).pl.show())
51-
52-
def test_plot_colorbar_can_adjust_title(self, sdata_blobs: SpatialData):
67+
fig, ax = plt.subplots()
5368
(
54-
sdata_blobs.pl.render_images(
55-
"blobs_image", channel=0, cmap="Reds", colorbar_params={"label": "Intensity"}
56-
).pl.show()
69+
sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"width": 0.4}).pl.show(
70+
ax=ax
71+
)
5772
)
73+
fig.tight_layout()
5874

59-
def test_plot_colorbar_can_adjust_pad(self, sdata_blobs: SpatialData):
60-
(sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"pad": 0.4}).pl.show())
75+
def test_plot_colorbar_can_adjust_title(self, sdata_blobs: SpatialData):
76+
fig, ax = plt.subplots()
77+
sdata_blobs.pl.render_images(
78+
"blobs_image", channel=0, cmap="Reds", colorbar_params={"label": "Intensity"}
79+
).pl.show(ax=ax)
80+
fig.tight_layout()
6181

62-
def test_plot_colorbar_can_have_colorbars_on_different_sides(self, sdata_blobs: SpatialData):
82+
def test_plot_colorbar_can_adjust_pad(self, sdata_blobs: SpatialData):
83+
fig, ax = plt.subplots()
6384
(
64-
sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "top"})
65-
.pl.render_labels("blobs_labels", color="instance_id", colorbar_params={"loc": "bottom"})
66-
.pl.show()
85+
sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds", colorbar_params={"pad": 0.4}).pl.show(
86+
ax=ax
87+
)
6788
)
89+
fig.tight_layout()
90+
91+
def test_plot_colorbar_can_have_colorbars_on_different_sides(self, sdata_blobs: SpatialData):
92+
fig, ax = plt.subplots()
93+
sdata_blobs.pl.render_images(
94+
"blobs_image", channel=0, cmap="Reds", colorbar_params={"loc": "top"}
95+
).pl.render_labels("blobs_labels", color="instance_id", colorbar_params={"loc": "bottom"}).pl.show(ax=ax)
96+
fig.tight_layout()
6897

6998
def test_plot_colorbar_can_have_two_colorbars_on_same_side(self, sdata_blobs: SpatialData):
70-
(
71-
sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds")
72-
.pl.render_labels("blobs_labels", color="instance_id")
73-
.pl.show()
74-
)
99+
fig, ax = plt.subplots()
100+
sdata_blobs.pl.render_images("blobs_image", channel=0, cmap="Reds").pl.render_labels(
101+
"blobs_labels", color="instance_id"
102+
).pl.show(ax=ax)
103+
fig.tight_layout()
75104

76105
def test_plot_colorbar_can_have_colorbars_on_all_sides(self, sdata_blobs: SpatialData):
77106
# primarily shows that spacing is correct between colorbars and plot elements
107+
fig, ax = plt.subplots()
78108
shared_params = {
79109
"element": "blobs_image",
80110
"channel": 0,
@@ -89,18 +119,25 @@ def test_plot_colorbar_can_have_colorbars_on_all_sides(self, sdata_blobs: Spatia
89119
.pl.render_images(**shared_params, colorbar_params={"loc": "right", "label": "right_2"})
90120
.pl.render_images(**shared_params, colorbar_params={"loc": "left", "label": "left_2"})
91121
.pl.render_images(**shared_params, colorbar_params={"loc": "bottom", "label": "bottom_2"})
92-
.pl.show()
122+
.pl.show(ax=ax)
93123
)
124+
fig.tight_layout()
94125

95126
def test_plot_multiple_images_in_one_cs_result_in_multiple_colorbars(self):
96127
sdata = self._make_multi_image_single_channel_sdata()
97-
sdata.pl.render_images(channel=0, cmap="Reds").pl.show()
128+
fig, ax = plt.subplots()
129+
sdata.pl.render_images(channel=0, cmap="Reds").pl.show(ax=ax)
130+
fig.tight_layout()
98131

99132
def test_plot_can_globally_turn_off_colorbars(self):
100133
# adresses https://github.com/scverse/spatialdata-plot/issues/431
101134
sdata = self._make_multi_image_single_channel_sdata()
102-
sdata.pl.render_images(channel=0, cmap="Reds").pl.show(colorbar=False)
135+
fig, ax = plt.subplots()
136+
sdata.pl.render_images(channel=0, cmap="Reds").pl.show(ax=ax, colorbar=False)
137+
fig.tight_layout()
103138

104139
def test_plot_single_channel_default_channel_name_omits_label(self):
105140
sdata = self._make_multi_image_single_channel_sdata()
106-
sdata.pl.render_images(element="img1", channel=0, cmap="Reds").pl.show()
141+
fig, ax = plt.subplots()
142+
sdata.pl.render_images(element="img1", channel=0, cmap="Reds").pl.show(ax=ax)
143+
fig.tight_layout()

0 commit comments

Comments
 (0)