Skip to content

Commit b104725

Browse files
authored
Merge pull request #27565 from jklymak/mnt-arghandling-subplotspec
MNT: arghandling subplotspec
2 parents 810a43b + 77c6325 commit b104725

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/matplotlib/gridspec.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,12 @@ def __init__(self, nrows, ncols,
484484
"""
485485
self._wspace = wspace
486486
self._hspace = hspace
487-
self._subplot_spec = subplot_spec
487+
if isinstance(subplot_spec, SubplotSpec):
488+
self._subplot_spec = subplot_spec
489+
else:
490+
raise TypeError(
491+
"subplot_spec must be type SubplotSpec, "
492+
"usually from GridSpec, or axes.get_subplotspec.")
488493
self.figure = self._subplot_spec.get_gridspec().figure
489494
super().__init__(nrows, ncols,
490495
width_ratios=width_ratios,

lib/matplotlib/tests/test_gridspec.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import matplotlib.gridspec as gridspec
2+
import matplotlib.pyplot as plt
23
import pytest
34

45

@@ -35,3 +36,15 @@ def test_repr():
3536
width_ratios=(1, 3))
3637
assert repr(ss) == \
3738
"GridSpec(2, 2, height_ratios=(3, 1), width_ratios=(1, 3))"
39+
40+
41+
def test_subplotspec_args():
42+
fig, axs = plt.subplots(1, 2)
43+
# should work:
44+
gs = gridspec.GridSpecFromSubplotSpec(2, 1,
45+
subplot_spec=axs[0].get_subplotspec())
46+
assert gs.get_topmost_subplotspec() == axs[0].get_subplotspec()
47+
with pytest.raises(TypeError, match="subplot_spec must be type SubplotSpec"):
48+
gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs[0])
49+
with pytest.raises(TypeError, match="subplot_spec must be type SubplotSpec"):
50+
gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs)

0 commit comments

Comments
 (0)