Skip to content

Commit 40862a3

Browse files
authored
Merge pull request #20150 from tacaswell/rename_mosaic_layout
Rename mosaic layout
2 parents cb756d3 + fa70002 commit 40862a3

File tree

4 files changed

+96
-68
lines changed

4 files changed

+96
-68
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Rename fist arg to subplot_mosaic
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Both `.FigureBase.subplot_mosaic`, and `.pyplot.subplot_mosaic` have had the
5+
first position argument renamed from *layout* to *mosaic*. This is because we
6+
are considering to consolidate *constrained_layout* and *tight_layout* keyword
7+
arguments in the Figure creation functions of `.pyplot` into a single *layout*
8+
keyword argument which would collide.
9+
10+
As this API is provisional, we are changing this with no deprecation period.

lib/matplotlib/figure.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ def _normalize_grid_string(layout):
16491649
layout = inspect.cleandoc(layout)
16501650
return [list(ln) for ln in layout.strip('\n').split('\n')]
16511651

1652-
def subplot_mosaic(self, layout, *, sharex=False, sharey=False,
1652+
def subplot_mosaic(self, mosaic, *, sharex=False, sharey=False,
16531653
subplot_kw=None, gridspec_kw=None, empty_sentinel='.'):
16541654
"""
16551655
Build a layout of Axes based on ASCII art or nested lists.
@@ -1663,7 +1663,7 @@ def subplot_mosaic(self, layout, *, sharex=False, sharey=False,
16631663
16641664
Parameters
16651665
----------
1666-
layout : list of list of {hashable or nested} or str
1666+
mosaic : list of list of {hashable or nested} or str
16671667
16681668
A visual layout of how you want your Axes to be arranged
16691669
labeled as strings. For example ::
@@ -1728,8 +1728,8 @@ def subplot_mosaic(self, layout, *, sharex=False, sharey=False,
17281728
subplot_kw = subplot_kw or {}
17291729
gridspec_kw = gridspec_kw or {}
17301730
# special-case string input
1731-
if isinstance(layout, str):
1732-
layout = self._normalize_grid_string(layout)
1731+
if isinstance(mosaic, str):
1732+
mosaic = self._normalize_grid_string(mosaic)
17331733
# Only accept strict bools to allow a possible future API expansion.
17341734
_api.check_isinstance(bool, sharex=sharex, sharey=sharey)
17351735

@@ -1749,10 +1749,10 @@ def _make_array(inp):
17491749
"""
17501750
r0, *rest = inp
17511751
if isinstance(r0, str):
1752-
raise ValueError('List layout specification must be 2D')
1752+
raise ValueError('List mosaic specification must be 2D')
17531753
for j, r in enumerate(rest, start=1):
17541754
if isinstance(r, str):
1755-
raise ValueError('List layout specification must be 2D')
1755+
raise ValueError('List mosaic specification must be 2D')
17561756
if len(r0) != len(r):
17571757
raise ValueError(
17581758
"All of the rows must be the same length, however "
@@ -1765,24 +1765,24 @@ def _make_array(inp):
17651765
out[j, k] = v
17661766
return out
17671767

1768-
def _identify_keys_and_nested(layout):
1768+
def _identify_keys_and_nested(mosaic):
17691769
"""
1770-
Given a 2D object array, identify unique IDs and nested layouts
1770+
Given a 2D object array, identify unique IDs and nested mosaics
17711771
17721772
Parameters
17731773
----------
1774-
layout : 2D numpy object array
1774+
mosaic : 2D numpy object array
17751775
17761776
Returns
17771777
-------
17781778
unique_ids : tuple
1779-
The unique non-sub layout entries in this layout
1779+
The unique non-sub mosaic entries in this mosaic
17801780
nested : dict[tuple[int, int]], 2D object array
17811781
"""
17821782
# make sure we preserve the user supplied order
17831783
unique_ids = cbook._OrderedSet()
17841784
nested = {}
1785-
for j, row in enumerate(layout):
1785+
for j, row in enumerate(mosaic):
17861786
for k, v in enumerate(row):
17871787
if v == empty_sentinel:
17881788
continue
@@ -1793,102 +1793,102 @@ def _identify_keys_and_nested(layout):
17931793

17941794
return tuple(unique_ids), nested
17951795

1796-
def _do_layout(gs, layout, unique_ids, nested):
1796+
def _do_layout(gs, mosaic, unique_ids, nested):
17971797
"""
1798-
Recursively do the layout.
1798+
Recursively do the mosaic.
17991799
18001800
Parameters
18011801
----------
18021802
gs : GridSpec
1803-
layout : 2D object array
1803+
mosaic : 2D object array
18041804
The input converted to a 2D numpy array for this level.
18051805
unique_ids : tuple
18061806
The identified scalar labels at this level of nesting.
18071807
nested : dict[tuple[int, int]], 2D object array
1808-
The identified nested layouts, if any.
1808+
The identified nested mosaics, if any.
18091809
18101810
Returns
18111811
-------
18121812
dict[label, Axes]
18131813
A flat dict of all of the Axes created.
18141814
"""
1815-
rows, cols = layout.shape
1815+
rows, cols = mosaic.shape
18161816
output = dict()
18171817

18181818
# we need to merge together the Axes at this level and the axes
1819-
# in the (recursively) nested sub-layouts so that we can add
1819+
# in the (recursively) nested sub-mosaics so that we can add
18201820
# them to the figure in the "natural" order if you were to
18211821
# ravel in c-order all of the Axes that will be created
18221822
#
18231823
# This will stash the upper left index of each object (axes or
1824-
# nested layout) at this level
1824+
# nested mosaic) at this level
18251825
this_level = dict()
18261826

18271827
# go through the unique keys,
18281828
for name in unique_ids:
18291829
# sort out where each axes starts/ends
1830-
indx = np.argwhere(layout == name)
1830+
indx = np.argwhere(mosaic == name)
18311831
start_row, start_col = np.min(indx, axis=0)
18321832
end_row, end_col = np.max(indx, axis=0) + 1
18331833
# and construct the slice object
18341834
slc = (slice(start_row, end_row), slice(start_col, end_col))
18351835
# some light error checking
1836-
if (layout[slc] != name).any():
1836+
if (mosaic[slc] != name).any():
18371837
raise ValueError(
1838-
f"While trying to layout\n{layout!r}\n"
1838+
f"While trying to layout\n{mosaic!r}\n"
18391839
f"we found that the label {name!r} specifies a "
18401840
"non-rectangular or non-contiguous area.")
18411841
# and stash this slice for later
18421842
this_level[(start_row, start_col)] = (name, slc, 'axes')
18431843

1844-
# do the same thing for the nested layouts (simpler because these
1844+
# do the same thing for the nested mosaics (simpler because these
18451845
# can not be spans yet!)
1846-
for (j, k), nested_layout in nested.items():
1847-
this_level[(j, k)] = (None, nested_layout, 'nested')
1846+
for (j, k), nested_mosaic in nested.items():
1847+
this_level[(j, k)] = (None, nested_mosaic, 'nested')
18481848

18491849
# now go through the things in this level and add them
18501850
# in order left-to-right top-to-bottom
18511851
for key in sorted(this_level):
18521852
name, arg, method = this_level[key]
18531853
# we are doing some hokey function dispatch here based
18541854
# on the 'method' string stashed above to sort out if this
1855-
# element is an axes or a nested layout.
1855+
# element is an axes or a nested mosaic.
18561856
if method == 'axes':
18571857
slc = arg
18581858
# add a single axes
18591859
if name in output:
18601860
raise ValueError(f"There are duplicate keys {name} "
1861-
f"in the layout\n{layout!r}")
1861+
f"in the layout\n{mosaic!r}")
18621862
ax = self.add_subplot(
18631863
gs[slc], **{'label': str(name), **subplot_kw}
18641864
)
18651865
output[name] = ax
18661866
elif method == 'nested':
1867-
nested_layout = arg
1867+
nested_mosaic = arg
18681868
j, k = key
1869-
# recursively add the nested layout
1870-
rows, cols = nested_layout.shape
1869+
# recursively add the nested mosaic
1870+
rows, cols = nested_mosaic.shape
18711871
nested_output = _do_layout(
18721872
gs[j, k].subgridspec(rows, cols, **gridspec_kw),
1873-
nested_layout,
1874-
*_identify_keys_and_nested(nested_layout)
1873+
nested_mosaic,
1874+
*_identify_keys_and_nested(nested_mosaic)
18751875
)
18761876
overlap = set(output) & set(nested_output)
18771877
if overlap:
18781878
raise ValueError(
18791879
f"There are duplicate keys {overlap} "
1880-
f"between the outer layout\n{layout!r}\n"
1881-
f"and the nested layout\n{nested_layout}"
1880+
f"between the outer layout\n{mosaic!r}\n"
1881+
f"and the nested layout\n{nested_mosaic}"
18821882
)
18831883
output.update(nested_output)
18841884
else:
18851885
raise RuntimeError("This should never happen")
18861886
return output
18871887

1888-
layout = _make_array(layout)
1889-
rows, cols = layout.shape
1888+
mosaic = _make_array(mosaic)
1889+
rows, cols = mosaic.shape
18901890
gs = self.add_gridspec(rows, cols, **gridspec_kw)
1891-
ret = _do_layout(gs, layout, *_identify_keys_and_nested(layout))
1891+
ret = _do_layout(gs, mosaic, *_identify_keys_and_nested(mosaic))
18921892
ax0 = next(iter(ret.values()))
18931893
for ax in ret.values():
18941894
if sharex:

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
14321432
return fig, axs
14331433

14341434

1435-
def subplot_mosaic(layout, *, sharex=False, sharey=False,
1435+
def subplot_mosaic(mosaic, *, sharex=False, sharey=False,
14361436
subplot_kw=None, gridspec_kw=None, empty_sentinel='.',
14371437
**fig_kw):
14381438
"""
@@ -1447,7 +1447,7 @@ def subplot_mosaic(layout, *, sharex=False, sharey=False,
14471447
14481448
Parameters
14491449
----------
1450-
layout : list of list of {hashable or nested} or str
1450+
mosaic : list of list of {hashable or nested} or str
14511451
14521452
A visual layout of how you want your Axes to be arranged
14531453
labeled as strings. For example ::
@@ -1513,7 +1513,7 @@ def subplot_mosaic(layout, *, sharex=False, sharey=False,
15131513
"""
15141514
fig = figure(**fig_kw)
15151515
ax_dict = fig.subplot_mosaic(
1516-
layout, sharex=sharex, sharey=sharey,
1516+
mosaic, sharex=sharex, sharey=sharey,
15171517
subplot_kw=subplot_kw, gridspec_kw=gridspec_kw,
15181518
empty_sentinel=empty_sentinel
15191519
)

0 commit comments

Comments
 (0)