Skip to content

Commit 30f3e57

Browse files
authored
set_map_layout: add more tests (#43)
* set_map_layout: add more tests * CHANGELOG
1 parent e7fca6e commit 30f3e57

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
* Added ``nrow`` and ``ncol`` parameters to ``set_map_layout`` for use with a
1414
gridspec.
15+
* Allow passing list of axes to ``set_map_layout``, renamed the files and extended
16+
the test coverage ([#42](https://github.com/mathause/mplotutils/pull/42)
17+
and [#43](https://github.com/mathause/mplotutils/pull/43)).
1518

1619
### Bug fixes
1720

@@ -21,7 +24,7 @@
2124

2225
### Internal changes
2326

24-
* Replaced `ax.get_subplotspec().get_geometry()` with `ax.get_subplotspec().get_geometry()`
27+
* Replaced `ax.get_geometry()` with `ax.get_subplotspec().get_geometry()`
2528
as the former was deprecated in matplotlib ([#8](https://github.com/mathause/mplotutils/pull/8)).
2629
* Refactor `mpu.cyclic_dataarray` using `obj.pad` ([#33](https://github.com/mathause/mplotutils/pull/33)).
2730
* Enabled CI on github actions ([#9](https://github.com/mathause/mplotutils/pull/9)).

mplotutils/tests/test_set_map_layout.py

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from . import subplots_context
77

88

9-
def test_default_width():
9+
def test_set_map_layout_default_width():
1010

1111
with subplots_context() as (f, ax):
1212
set_map_layout(ax)
@@ -17,7 +17,7 @@ def test_default_width():
1717
@pytest.mark.parametrize(
1818
"nrow_ncol", [{"nrow": None, "ncol": None}, {"nrow": 1, "ncol": 1}]
1919
)
20-
def test_no_borders(nrow_ncol):
20+
def test_set_map_layout_no_borders(nrow_ncol):
2121

2222
# width:height = 1:1
2323
with subplots_context() as (f, ax):
@@ -50,7 +50,21 @@ def test_no_borders(nrow_ncol):
5050
assert np.allclose((width, height), (10, 20))
5151

5252

53-
def test_vert_borders():
53+
@pytest.mark.parametrize("ax_to_arr", (lambda ax: [ax], lambda ax: np.array(ax)))
54+
def test_set_map_layout_ax_arr(ax_to_arr):
55+
56+
# width:height = 1:1
57+
with subplots_context() as (f, ax):
58+
ax.set_aspect("equal")
59+
ax.set(xlim=(0, 1), ylim=(0, 1))
60+
f.subplots_adjust(left=0, bottom=0, right=1, top=1)
61+
set_map_layout(ax_to_arr(ax), 10)
62+
63+
width, height = f.get_size_inches() * 2.54
64+
assert np.allclose((width, height), (10, 10))
65+
66+
67+
def test_set_map_layout_vert_borders():
5468

5569
# width:height = 1:1
5670
with subplots_context() as (f, ax):
@@ -89,7 +103,7 @@ def test_vert_borders():
89103
assert np.allclose((width, height), (10, 40))
90104

91105

92-
def test_horz_borders():
106+
def test_set_map_layout_horz_borders():
93107

94108
# width:height = 1:1
95109
with subplots_context() as (f, ax):
@@ -116,7 +130,65 @@ def test_horz_borders():
116130
assert np.allclose((width, height), (10, 5))
117131

118132

119-
def test_nrow_ncol_only_one_raises():
133+
def test_set_map_layout_two_axes_vert():
134+
135+
# width:height = 1:1
136+
with subplots_context(2, 1) as (f, axs):
137+
for ax in axs:
138+
ax.set_aspect("equal")
139+
ax.set(xlim=(0, 1), ylim=(0, 1))
140+
141+
f.subplots_adjust(left=0, bottom=0, right=1, top=1, hspace=0)
142+
143+
set_map_layout(ax, 10)
144+
145+
width, height = f.get_size_inches() * 2.54
146+
assert np.allclose((width, height), (10, 20))
147+
148+
# width:height = 1:1
149+
with subplots_context(2, 1) as (f, axs):
150+
for ax in axs:
151+
ax.set_aspect("equal")
152+
ax.set(xlim=(0, 1), ylim=(0, 1))
153+
154+
f.subplots_adjust(left=0, bottom=0, right=1, top=1, hspace=1)
155+
156+
set_map_layout(ax, 10)
157+
158+
width, height = f.get_size_inches() * 2.54
159+
assert np.allclose((width, height), (10, 30))
160+
161+
162+
def test_set_map_layout_two_axes_horz():
163+
164+
# width:height = 1:1
165+
with subplots_context(1, 2) as (f, axs):
166+
for ax in axs:
167+
ax.set_aspect("equal")
168+
ax.set(xlim=(0, 1), ylim=(0, 1))
169+
170+
f.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0)
171+
172+
set_map_layout(ax, 10)
173+
174+
width, height = f.get_size_inches() * 2.54
175+
assert np.allclose((width, height), (10, 5))
176+
177+
# width:height = 1:1
178+
with subplots_context(1, 2) as (f, axs):
179+
for ax in axs:
180+
ax.set_aspect("equal")
181+
ax.set(xlim=(0, 1), ylim=(0, 1))
182+
183+
f.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=1)
184+
185+
set_map_layout(ax, 10)
186+
187+
width, height = f.get_size_inches() * 2.54
188+
assert np.allclose((width, height), (10, 10 / 3))
189+
190+
191+
def test_set_map_layout_nrow_ncol_only_one_raises():
120192

121193
with pytest.raises(ValueError, match="Must set none or both of 'nrow' and 'ncol'"):
122194
set_map_layout(None, width=17.0, nrow=1, ncol=None)

0 commit comments

Comments
 (0)