Skip to content

Commit 5755c66

Browse files
authored
cartopy data ratio requires draw (#61)
* cartopy data ratio requires draw * call draw * changelog
1 parent a12b024 commit 5755c66

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bug fixes
1010

11+
- Fixed a bug in `set_map_layout`: the data ratio of a cartopy `GeoAxesSubplot` requires
12+
a `draw` to be correct ([#61](https://github.com/mathause/mplotutils/pull/61)).
1113
- Fix a regression introduced in [#33](https://github.com/mathause/mplotutils/pull/33):
1214
`cyclic_dataarray` now correctly extrapolates the coordinates
1315
([#58](https://github.com/mathause/mplotutils/pull/58)).

mplotutils/map_layout.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def set_map_layout(axes, width=17.0, *, nrow=None, ncol=None):
4040
# read figure data
4141
f = ax.get_figure()
4242

43+
# getting the correct data ratio of geoaxes requires draw
44+
f.canvas.draw()
45+
4346
bottom = f.subplotpars.bottom
4447
top = f.subplotpars.top
4548
left = f.subplotpars.left

mplotutils/tests/test_set_map_layout.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,20 @@ def test_set_map_layout_nrow_ncol_only_one_raises():
195195

196196
with pytest.raises(ValueError, match="Must set none or both of 'nrow' and 'ncol'"):
197197
set_map_layout(None, width=17.0, nrow=None, ncol=1)
198+
199+
200+
def test_set_map_layout_cartopy_2_2():
201+
202+
import cartopy.crs as ccrs
203+
204+
subplot_kw = {"projection": ccrs.PlateCarree()}
205+
with subplots_context(2, 2, subplot_kw=subplot_kw) as (f, axs):
206+
207+
f.subplots_adjust(hspace=0, wspace=0, top=1, bottom=0, left=0, right=1)
208+
209+
set_map_layout(axs, width=17) # width is in cm
210+
211+
result = f.get_size_inches() * 2.54
212+
expected = (17, 8.5)
213+
214+
np.testing.assert_allclose(result, expected)

0 commit comments

Comments
 (0)