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

Skip to content

Commit 09dfa2e

Browse files
authored
Merge pull request #20178 from tacaswell/auto-backport-of-pr-20150-on-v3.4.x
Backport PR #20150 on branch v3.4.x
2 parents f498fdd + de8dc8a commit 09dfa2e

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
@@ -1674,7 +1674,7 @@ def _normalize_grid_string(layout):
16741674
layout = inspect.cleandoc(layout)
16751675
return [list(ln) for ln in layout.strip('\n').split('\n')]
16761676

1677-
def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
1677+
def subplot_mosaic(self, mosaic, *, subplot_kw=None, gridspec_kw=None,
16781678
empty_sentinel='.'):
16791679
"""
16801680
Build a layout of Axes based on ASCII art or nested lists.
@@ -1689,7 +1689,7 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
16891689
16901690
Parameters
16911691
----------
1692-
layout : list of list of {hashable or nested} or str
1692+
mosaic : list of list of {hashable or nested} or str
16931693
16941694
A visual layout of how you want your Axes to be arranged
16951695
labeled as strings. For example ::
@@ -1748,8 +1748,8 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
17481748
subplot_kw = subplot_kw or {}
17491749
gridspec_kw = gridspec_kw or {}
17501750
# special-case string input
1751-
if isinstance(layout, str):
1752-
layout = self._normalize_grid_string(layout)
1751+
if isinstance(mosaic, str):
1752+
mosaic = self._normalize_grid_string(mosaic)
17531753

17541754
def _make_array(inp):
17551755
"""
@@ -1767,10 +1767,10 @@ def _make_array(inp):
17671767
"""
17681768
r0, *rest = inp
17691769
if isinstance(r0, str):
1770-
raise ValueError('List layout specification must be 2D')
1770+
raise ValueError('List mosaic specification must be 2D')
17711771
for j, r in enumerate(rest, start=1):
17721772
if isinstance(r, str):
1773-
raise ValueError('List layout specification must be 2D')
1773+
raise ValueError('List mosaic specification must be 2D')
17741774
if len(r0) != len(r):
17751775
raise ValueError(
17761776
"All of the rows must be the same length, however "
@@ -1783,24 +1783,24 @@ def _make_array(inp):
17831783
out[j, k] = v
17841784
return out
17851785

1786-
def _identify_keys_and_nested(layout):
1786+
def _identify_keys_and_nested(mosaic):
17871787
"""
1788-
Given a 2D object array, identify unique IDs and nested layouts
1788+
Given a 2D object array, identify unique IDs and nested mosaics
17891789
17901790
Parameters
17911791
----------
1792-
layout : 2D numpy object array
1792+
mosaic : 2D numpy object array
17931793
17941794
Returns
17951795
-------
17961796
unique_ids : tuple
1797-
The unique non-sub layout entries in this layout
1797+
The unique non-sub mosaic entries in this mosaic
17981798
nested : dict[tuple[int, int]], 2D object array
17991799
"""
18001800
# make sure we preserve the user supplied order
18011801
unique_ids = cbook._OrderedSet()
18021802
nested = {}
1803-
for j, row in enumerate(layout):
1803+
for j, row in enumerate(mosaic):
18041804
for k, v in enumerate(row):
18051805
if v == empty_sentinel:
18061806
continue
@@ -1811,102 +1811,102 @@ def _identify_keys_and_nested(layout):
18111811

18121812
return tuple(unique_ids), nested
18131813

1814-
def _do_layout(gs, layout, unique_ids, nested):
1814+
def _do_layout(gs, mosaic, unique_ids, nested):
18151815
"""
1816-
Recursively do the layout.
1816+
Recursively do the mosaic.
18171817
18181818
Parameters
18191819
----------
18201820
gs : GridSpec
1821-
layout : 2D object array
1821+
mosaic : 2D object array
18221822
The input converted to a 2D numpy array for this level.
18231823
unique_ids : tuple
18241824
The identified scalar labels at this level of nesting.
18251825
nested : dict[tuple[int, int]], 2D object array
1826-
The identified nested layouts, if any.
1826+
The identified nested mosaics, if any.
18271827
18281828
Returns
18291829
-------
18301830
dict[label, Axes]
18311831
A flat dict of all of the Axes created.
18321832
"""
1833-
rows, cols = layout.shape
1833+
rows, cols = mosaic.shape
18341834
output = dict()
18351835

18361836
# we need to merge together the Axes at this level and the axes
1837-
# in the (recursively) nested sub-layouts so that we can add
1837+
# in the (recursively) nested sub-mosaics so that we can add
18381838
# them to the figure in the "natural" order if you were to
18391839
# ravel in c-order all of the Axes that will be created
18401840
#
18411841
# This will stash the upper left index of each object (axes or
1842-
# nested layout) at this level
1842+
# nested mosaic) at this level
18431843
this_level = dict()
18441844

18451845
# go through the unique keys,
18461846
for name in unique_ids:
18471847
# sort out where each axes starts/ends
1848-
indx = np.argwhere(layout == name)
1848+
indx = np.argwhere(mosaic == name)
18491849
start_row, start_col = np.min(indx, axis=0)
18501850
end_row, end_col = np.max(indx, axis=0) + 1
18511851
# and construct the slice object
18521852
slc = (slice(start_row, end_row), slice(start_col, end_col))
18531853
# some light error checking
1854-
if (layout[slc] != name).any():
1854+
if (mosaic[slc] != name).any():
18551855
raise ValueError(
1856-
f"While trying to layout\n{layout!r}\n"
1856+
f"While trying to layout\n{mosaic!r}\n"
18571857
f"we found that the label {name!r} specifies a "
18581858
"non-rectangular or non-contiguous area.")
18591859
# and stash this slice for later
18601860
this_level[(start_row, start_col)] = (name, slc, 'axes')
18611861

1862-
# do the same thing for the nested layouts (simpler because these
1862+
# do the same thing for the nested mosaics (simpler because these
18631863
# can not be spans yet!)
1864-
for (j, k), nested_layout in nested.items():
1865-
this_level[(j, k)] = (None, nested_layout, 'nested')
1864+
for (j, k), nested_mosaic in nested.items():
1865+
this_level[(j, k)] = (None, nested_mosaic, 'nested')
18661866

18671867
# now go through the things in this level and add them
18681868
# in order left-to-right top-to-bottom
18691869
for key in sorted(this_level):
18701870
name, arg, method = this_level[key]
18711871
# we are doing some hokey function dispatch here based
18721872
# on the 'method' string stashed above to sort out if this
1873-
# element is an axes or a nested layout.
1873+
# element is an axes or a nested mosaic.
18741874
if method == 'axes':
18751875
slc = arg
18761876
# add a single axes
18771877
if name in output:
18781878
raise ValueError(f"There are duplicate keys {name} "
1879-
f"in the layout\n{layout!r}")
1879+
f"in the layout\n{mosaic!r}")
18801880
ax = self.add_subplot(
18811881
gs[slc], **{'label': str(name), **subplot_kw}
18821882
)
18831883
output[name] = ax
18841884
elif method == 'nested':
1885-
nested_layout = arg
1885+
nested_mosaic = arg
18861886
j, k = key
1887-
# recursively add the nested layout
1888-
rows, cols = nested_layout.shape
1887+
# recursively add the nested mosaic
1888+
rows, cols = nested_mosaic.shape
18891889
nested_output = _do_layout(
18901890
gs[j, k].subgridspec(rows, cols, **gridspec_kw),
1891-
nested_layout,
1892-
*_identify_keys_and_nested(nested_layout)
1891+
nested_mosaic,
1892+
*_identify_keys_and_nested(nested_mosaic)
18931893
)
18941894
overlap = set(output) & set(nested_output)
18951895
if overlap:
18961896
raise ValueError(
18971897
f"There are duplicate keys {overlap} "
1898-
f"between the outer layout\n{layout!r}\n"
1899-
f"and the nested layout\n{nested_layout}"
1898+
f"between the outer layout\n{mosaic!r}\n"
1899+
f"and the nested layout\n{nested_mosaic}"
19001900
)
19011901
output.update(nested_output)
19021902
else:
19031903
raise RuntimeError("This should never happen")
19041904
return output
19051905

1906-
layout = _make_array(layout)
1907-
rows, cols = layout.shape
1906+
mosaic = _make_array(mosaic)
1907+
rows, cols = mosaic.shape
19081908
gs = self.add_gridspec(rows, cols, **gridspec_kw)
1909-
ret = _do_layout(gs, layout, *_identify_keys_and_nested(layout))
1909+
ret = _do_layout(gs, mosaic, *_identify_keys_and_nested(mosaic))
19101910
for k, ax in ret.items():
19111911
if isinstance(k, str):
19121912
ax.set_label(k)

lib/matplotlib/pyplot.py

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

14451445

1446-
def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
1446+
def subplot_mosaic(mosaic, *, subplot_kw=None, gridspec_kw=None,
14471447
empty_sentinel='.', **fig_kw):
14481448
"""
14491449
Build a layout of Axes based on ASCII art or nested lists.
@@ -1458,7 +1458,7 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
14581458
14591459
Parameters
14601460
----------
1461-
layout : list of list of {hashable or nested} or str
1461+
mosaic : list of list of {hashable or nested} or str
14621462
14631463
A visual layout of how you want your Axes to be arranged
14641464
labeled as strings. For example ::
@@ -1518,7 +1518,7 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
15181518
"""
15191519
fig = figure(**fig_kw)
15201520
ax_dict = fig.subplot_mosaic(
1521-
layout,
1521+
mosaic,
15221522
subplot_kw=subplot_kw,
15231523
gridspec_kw=gridspec_kw,
15241524
empty_sentinel=empty_sentinel

0 commit comments

Comments
 (0)