From e03a2f9ec5c1e1c527d1c0d4c19e6efeffc5356d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 30 Dec 2019 22:44:44 +0100 Subject: [PATCH] Add __repr__ to SubplotSpec. This allows printing subplotspecs as e.g. `GridSpec(3, 3)[1:3, 2:3]` which can help with debugging. (Note that `GridSpec.__repr__` was already implemented previously.) --- lib/matplotlib/gridspec.py | 5 +++++ lib/matplotlib/tests/test_gridspec.py | 5 +++++ 2 files changed, 10 insertions(+) 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]"