Skip to content

Commit 469cfbe

Browse files
committed
Single-line string notation for subplot_mosaic
1 parent cf83ec4 commit 469cfbe

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/matplotlib/figure.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,8 +1776,13 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None):
17761776

17771777
@staticmethod
17781778
def _normalize_grid_string(layout):
1779-
layout = inspect.cleandoc(layout)
1780-
return [list(ln) for ln in layout.strip('\n').split('\n')]
1779+
if '\n' not in layout:
1780+
# single-line string
1781+
return [list(ln) for ln in layout.split(';')]
1782+
else:
1783+
# multi-line string
1784+
layout = inspect.cleandoc(layout)
1785+
return [list(ln) for ln in layout.strip('\n').split('\n')]
17811786

17821787
def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
17831788
empty_sentinel='.'):
@@ -1812,16 +1817,21 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
18121817
Any of the entries in the layout can be a list of lists
18131818
of the same form to create nested layouts.
18141819
1815-
If input is a str, then it must be of the form ::
1820+
If input is a str, then it can either be a multi-line string of
1821+
the form ::
18161822
18171823
'''
18181824
AAE
18191825
C.E
18201826
'''
18211827
1822-
where each character is a column and each line is a row.
1823-
This only allows only single character Axes labels and does
1824-
not allow nesting but is very terse.
1828+
where each character is a column and each line is a row. Or it
1829+
can be a single-line string where rows are separated by ``;``::
1830+
1831+
'AB;CC'
1832+
1833+
The string notation allows only single character Axes labels and
1834+
does not support nesting but is very terse.
18251835
18261836
subplot_kw : dict, optional
18271837
Dictionary with keywords passed to the `.Figure.add_subplot` call

lib/matplotlib/tests/test_figure.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,25 @@ def test_subplot_kw(self, fig_test, fig_ref, subplot_kw):
758758

759759
axB = fig_ref.add_subplot(gs[0, 1], **subplot_kw)
760760

761+
def test_string_parser(self):
762+
normalize = Figure._normalize_grid_string
763+
assert normalize('ABC') == [['A', 'B', 'C']]
764+
assert normalize('AB;CC') == [['A', 'B'], ['C', 'C']]
765+
assert normalize('AB;CC;DE') == [['A', 'B'], ['C', 'C'], ['D', 'E']]
766+
assert normalize("""
767+
ABC
768+
""") == [['A', 'B', 'C']]
769+
assert normalize("""
770+
AB
771+
CC
772+
""") == [['A', 'B'], ['C', 'C']]
773+
assert normalize("""
774+
AB
775+
CC
776+
DE
777+
""") == [['A', 'B'], ['C', 'C'], ['D', 'E']]
778+
779+
761780
@check_figures_equal(extensions=["png"])
762781
@pytest.mark.parametrize("str_pattern",
763782
["AAA\nBBB", "\nAAA\nBBB\n", "ABC\nDEF"]

0 commit comments

Comments
 (0)