Repository: mathause/mplotutils

Issue:
aspect ratio requires draw for cartopy maps
The following leads to a wrong figure size:

```
f, axs = plt.subplots(2, 1, subplot_kw=dict(projection=ccrs.Robinson()))
f.subplots_adjust(hspace=0, wspace=0, top=1, bottom=0, left=0, right=1)
mpu.set_map_layout(axs, width=17)  # width is in cm
print("Size (w x h):  ", np.round(f.get_size_inches() * 2.54, 2))
```

cartopy maps seem to require a `f.canvas.draw()` command




Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/mathause/mplotutils/pull/61?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser) Report
Base: **94.68**% // Head: **94.80**% // Increases project coverage by **`+0.12%`** :tada:
> Coverage data is based on head [(`f45f66b`)](https://codecov.io/gh/mathause/mplotutils/pull/61?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser) compared to base [(`40b30d8`)](https://codecov.io/gh/mathause/mplotutils/commit/40b30d8ea14acad33341b472dff84fdb73777bc7?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser).
> Patch coverage: 100.00% of modified lines in pull request are covered.

:mega: This organization is not using Codecov’s [GitHub App Integration](https://github.com/apps/codecov). We recommend you install it so Codecov can continue to function properly for your repositories. [Learn more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser)

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main      #61      +/-   ##
==========================================
+ Coverage   94.68%   94.80%   +0.12%     
==========================================
  Files           7        7              
  Lines         376      385       +9     
==========================================
+ Hits          356      365       +9     
  Misses         20       20              
```

| Flag | Coverage Δ | |
|---|---|---|
| min-all-deps | `94.79% <100.00%> (+0.12%)` | :arrow_up: |
| unittests | `94.80% <100.00%> (+0.12%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/mathause/mplotutils/pull/61?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser) | Coverage Δ | |
|---|---|---|
| [mplotutils/cartopy\_utils.py](https://codecov.io/gh/mathause/mplotutils/pull/61?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser#diff-bXBsb3R1dGlscy9jYXJ0b3B5X3V0aWxzLnB5) | `86.88% <100.00%> (+0.92%)` | :arrow_up: |
| [mplotutils/map\_layout.py](https://codecov.io/gh/mathause/mplotutils/pull/61?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser#diff-bXBsb3R1dGlscy9tYXBfbGF5b3V0LnB5) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/mathause/mplotutils/pull/61?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Mathias+Hauser).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 023cb81..3afab67 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@
 
 ### Bug fixes
 
+- Fixed a bug in `set_map_layout`: the data ratio of a cartopy `GeoAxesSubplot` requires
+  a `draw` to be correct ([#61](https://github.com/mathause/mplotutils/pull/61)).
 - Fix a regression introduced in [#33](https://github.com/mathause/mplotutils/pull/33):
   `cyclic_dataarray` now correctly extrapolates the coordinates
   ([#58](https://github.com/mathause/mplotutils/pull/58)).
diff --git a/mplotutils/map_layout.py b/mplotutils/map_layout.py
index 33ce41b..ab620a0 100644
--- a/mplotutils/map_layout.py
+++ b/mplotutils/map_layout.py
@@ -40,6 +40,9 @@ def set_map_layout(axes, width=17.0, *, nrow=None, ncol=None):
     # read figure data
     f = ax.get_figure()
 
+    # getting the correct data ratio of geoaxes requires draw
+    f.canvas.draw()
+
     bottom = f.subplotpars.bottom
     top = f.subplotpars.top
     left = f.subplotpars.left


Test patch:
diff --git a/mplotutils/tests/test_set_map_layout.py b/mplotutils/tests/test_set_map_layout.py
index 83aba3c..0ff4e10 100644
--- a/mplotutils/tests/test_set_map_layout.py
+++ b/mplotutils/tests/test_set_map_layout.py
@@ -195,3 +195,20 @@ def test_set_map_layout_nrow_ncol_only_one_raises():
 
     with pytest.raises(ValueError, match="Must set none or both of 'nrow' and 'ncol'"):
         set_map_layout(None, width=17.0, nrow=None, ncol=1)
+
+
+def test_set_map_layout_cartopy_2_2():
+
+    import cartopy.crs as ccrs
+
+    subplot_kw = {"projection": ccrs.PlateCarree()}
+    with subplots_context(2, 2, subplot_kw=subplot_kw) as (f, axs):
+
+        f.subplots_adjust(hspace=0, wspace=0, top=1, bottom=0, left=0, right=1)
+
+        set_map_layout(axs, width=17)  # width is in cm
+
+        result = f.get_size_inches() * 2.54
+        expected = (17, 8.5)
+
+        np.testing.assert_allclose(result, expected)