Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e03a2f9

Browse files
committed
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.)
1 parent 160f711 commit e03a2f9

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/matplotlib/gridspec.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,11 @@ def __init__(self, gridspec, num1, num2=None):
522522
else:
523523
self._layoutbox = None
524524

525+
def __repr__(self):
526+
return (f"{self.get_gridspec()}["
527+
f"{self.rowspan.start}:{self.rowspan.stop}, "
528+
f"{self.colspan.start}:{self.colspan.stop}]")
529+
525530
@staticmethod
526531
def _from_subplot_args(figure, args):
527532
"""

lib/matplotlib/tests/test_gridspec.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ def test_height_ratios():
2424
"""
2525
with pytest.raises(ValueError):
2626
gridspec.GridSpec(1, 1, height_ratios=[2, 1, 3])
27+
28+
29+
def test_repr():
30+
ss = gridspec.GridSpec(3, 3)[2, 1:3]
31+
assert repr(ss) == "GridSpec(3, 3)[2:3, 1:3]"

0 commit comments

Comments
 (0)