diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 678f03e4a647..29c8f2d6864d 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -522,6 +522,11 @@ def __init__(self, gridspec, num1, num2=None): else: self._layoutbox = None + def __repr__(self): + return (f"{self.get_gridspec()}[" + f"{self.rowspan.start}:{self.rowspan.stop}, " + f"{self.colspan.start}:{self.colspan.stop}]") + @staticmethod def _from_subplot_args(figure, args): """ diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 70d1ee132851..fa931ce086e5 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -24,3 +24,8 @@ def test_height_ratios(): """ with pytest.raises(ValueError): gridspec.GridSpec(1, 1, height_ratios=[2, 1, 3]) + + +def test_repr(): + ss = gridspec.GridSpec(3, 3)[2, 1:3] + assert repr(ss) == "GridSpec(3, 3)[2:3, 1:3]"