Skip to content

Commit 0a26275

Browse files
authored
set_map_layout: better errors for wrong type and subfigure (#121)
* set_map_layout: better errors for wrong type and subfigure * CHANGELOG
1 parent 882b68d commit 0a26275

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
### Enhancements
1414

1515
- Enable passing `AxesGrid` (from `mpl_toolkits.axes_grid1`) to `set_map_layout` ([#116](https://github.com/mathause/mplotutils/pull/116)).
16+
- Raise more informative error when a wrong type is passed to `set_map_layout` ([#121](https://github.com/mathause/mplotutils/pull/121)).
17+
- `set_map_layout` now raises an explicit error when the figure contains SubFigure ([#121](https://github.com/mathause/mplotutils/pull/121)).
1618

1719
## v0.5.0 (27.03.2024)
1820

mplotutils/map_layout.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import warnings
22

3+
import matplotlib as mpl
34
import matplotlib.pyplot as plt
45
import numpy as np
56
from mpl_toolkits.axes_grid1 import AxesGrid
@@ -50,20 +51,23 @@ def set_map_layout(obj=None, width=17.0, *, nrow=None, ncol=None, axes=None):
5051
_set_map_layout_axes(obj, width, nrow, ncol)
5152

5253

53-
def _set_map_layout_axes(axes, width, nrow, ncol):
54+
def _set_map_layout_axes(axs, width, nrow, ncol):
5455

5556
if (nrow is None and ncol is not None) or (nrow is not None and ncol is None):
5657
raise ValueError("Must set none or both of 'nrow' and 'ncol'")
5758

58-
if isinstance(axes, plt.Axes):
59-
ax = axes
60-
else:
61-
# assumes the first of the axes is representative for all
62-
ax = np.asarray(axes).flat[0]
59+
# assumes the first of the axes is representative for all
60+
ax = np.asarray(axs).flat[0]
61+
62+
if not isinstance(ax, plt.Axes):
63+
raise TypeError(f"Expected axes or an array of axes, got {type(ax)}")
6364

6465
# read figure data
6566
f = ax.get_figure()
6667

68+
if isinstance(f, mpl.figure.SubFigure) or f.subfigs:
69+
raise RuntimeError("matplotlib SubFigure not supported")
70+
6771
# getting the correct data ratio of geoaxes requires draw
6872
f.canvas.draw()
6973

mplotutils/tests/test_set_map_layout.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ def test_set_map_layout_deprecated_kwarg():
2525
set_map_layout(object, axes=object)
2626

2727

28+
def test_map_layout_not_axes_error():
29+
30+
with figure_context() as f:
31+
with pytest.raises(TypeError, match="Expected axes or an array of axes"):
32+
set_map_layout(f)
33+
34+
with pytest.raises(TypeError, match="Expected axes or an array of axes"):
35+
set_map_layout(object)
36+
37+
38+
def test_map_layout_subfigures_error():
39+
40+
with figure_context() as f:
41+
sf = f.subfigures(1, 1)
42+
axs = sf.subplots(1, 2)
43+
44+
with pytest.raises(RuntimeError, match="matplotlib SubFigure not supported"):
45+
set_map_layout(f.axes)
46+
47+
with pytest.raises(RuntimeError, match="matplotlib SubFigure not supported"):
48+
set_map_layout(axs)
49+
50+
2851
def test_set_map_layout_default_width():
2952
with subplots_context() as (f, ax):
3053
set_map_layout(ax)

0 commit comments

Comments
 (0)