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

Skip to content

Commit fb45567

Browse files
committed
MNT: adjust how to recursively generate + fix rebase issues
1 parent 8bdc4da commit fb45567

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

lib/matplotlib/figure.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ def _normalize_grid_string(layout):
18961896
layout = inspect.cleandoc(layout)
18971897
return [list(ln) for ln in layout.strip('\n').split('\n')]
18981898

1899-
def _sub_prep(self, mosaic, sub_add_func, *, sharex=False, sharey=False,
1899+
def _sub_prep(self, mosaic, parent, mode='axes', *,
19001900
width_ratios=None, height_ratios=None,
19011901
empty_sentinel='.',
19021902
subthing_kw=None, per_subthing_kw=None, gridspec_kw=None):
@@ -1969,15 +1969,12 @@ def _sub_prep(self, mosaic, sub_add_func, *, sharex=False, sharey=False,
19691969
# special-case string input
19701970
if isinstance(mosaic, str):
19711971
mosaic = self._normalize_grid_string(mosaic)
1972-
per_subplot_kw = {
1973-
tuple(k): v for k, v in per_subplot_kw.items()
1972+
per_subthing_kw = {
1973+
tuple(k): v for k, v in per_subthing_kw.items()
19741974
}
19751975

19761976
per_subthing_kw = self._check_duplication_and_flatten_kwargs(per_subthing_kw)
19771977

1978-
# Only accept strict bools to allow a possible future API expansion.
1979-
_api.check_isinstance(bool, sharex=sharex, sharey=sharey)
1980-
19811978
def _make_array(inp):
19821979
"""
19831980
Convert input into 2D array
@@ -2047,7 +2044,7 @@ def _identify_keys_and_nested(mosaic):
20472044

20482045
return tuple(unique_ids), nested
20492046

2050-
def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
2047+
def _do_layout(gs, mosaic, unique_ids, nested, parent, mode):
20512048
"""
20522049
Recursively do the mosaic.
20532050
@@ -2080,13 +2077,6 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
20802077
# nested mosaic) at this level
20812078
this_level = dict()
20822079

2083-
# When dealing with .add_subfigure - need to create
2084-
# separate SubFigures for nested layouts,
2085-
# otherwise they will all create without nesting.
2086-
subfigs_for_nested = {}
2087-
if sub_add_func.__name__ == 'add_subfigure':
2088-
subfigs_for_nested = _get_subfigs_from_nested(nested, sub_add_func, gs)
2089-
20902080
# go through the unique keys,
20912081
for name in unique_ids:
20922082
# sort out where each axes starts/ends
@@ -2121,6 +2111,10 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
21212111
if name in output:
21222112
raise ValueError(f"There are duplicate keys {name} "
21232113
f"in the layout\n{mosaic!r}")
2114+
if mode == 'axes':
2115+
sub_add_func = parent.add_subplot
2116+
elif mode == 'subfigures':
2117+
sub_add_func = parent.add_subfigure
21242118
ax = sub_add_func(
21252119
gs[slc], **{
21262120
'label': str(name),
@@ -2135,12 +2129,18 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
21352129
j, k = key
21362130
# recursively add the nested mosaic
21372131
rows, cols = nested_mosaic.shape
2132+
if mode == 'subfigures':
2133+
local_parent = parent.add_subfigure(gs[j, k])
2134+
elif mode == 'axes':
2135+
local_parent = parent
2136+
else:
2137+
raise ValueError
21382138
nested_output = _do_layout(
21392139
gs[j, k].subgridspec(rows, cols),
21402140
nested_mosaic,
21412141
*_identify_keys_and_nested(nested_mosaic),
2142-
sub_add_func if not subfigs_for_nested.get(key)
2143-
else subfigs_for_nested[key].add_subfigure
2142+
local_parent,
2143+
mode
21442144
)
21452145
overlap = set(output) & set(nested_output)
21462146
if overlap:
@@ -2157,22 +2157,17 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
21572157
mosaic = _make_array(mosaic)
21582158
rows, cols = mosaic.shape
21592159
gs = self.add_gridspec(rows, cols, **gridspec_kw)
2160-
ret = _do_layout(gs, mosaic, *_identify_keys_and_nested(mosaic))
2161-
ax0 = next(iter(ret.values()))
2162-
for ax in ret.values():
2163-
if sharex:
2164-
ax.sharex(ax0)
2165-
ax._label_outer_xaxis(skip_non_rectangular_axes=True)
2166-
if sharey:
2167-
ax.sharey(ax0)
2168-
ax._label_outer_yaxis(skip_non_rectangular_axes=True)
2169-
if extra := set(per_subplot_kw) - set(ret):
2160+
ret = _do_layout(gs, mosaic, *_identify_keys_and_nested(mosaic), parent, mode)
2161+
2162+
if extra := set(per_subthing_kw) - set(ret):
21702163
raise ValueError(
21712164
f"The keys {extra} are in *per_subplot_kw* "
21722165
"but not in the mosaic."
21732166
)
21742167

2175-
def subfigure_mosaic(self, mosaic, *, width_ratios=None,
2168+
return ret
2169+
2170+
def subfigure_mosaic(self, mosaic, *, sharex=False, sharey=False, width_ratios=None,
21762171
height_ratios=None, empty_sentinel='.',
21772172
subfigure_kw=None, per_subfigure_kw=None, gridspec_kw=None):
21782173
"""
@@ -2271,7 +2266,8 @@ def subfigure_mosaic(self, mosaic, *, width_ratios=None,
22712266
total layout.
22722267
22732268
"""
2274-
ret = self._sub_prep(mosaic, self.add_subfigure, width_ratios=width_ratios,
2269+
2270+
ret = self._sub_prep(mosaic, self, 'subfigures', width_ratios=width_ratios,
22752271
height_ratios=height_ratios, empty_sentinel=empty_sentinel,
22762272
subthing_kw=subfigure_kw, per_subthing_kw=per_subfigure_kw,
22772273
gridspec_kw=gridspec_kw)
@@ -2387,10 +2383,24 @@ def subplot_mosaic(self, mosaic, *, sharex=False, sharey=False,
23872383
total layout.
23882384
23892385
"""
2390-
ret = self._sub_prep(mosaic, self.add_subplot, sharex=sharex, sharey=sharey,
2386+
2387+
# Only accept strict bools to allow a possible future API expansion.
2388+
_api.check_isinstance(bool, sharex=sharex, sharey=sharey)
2389+
2390+
ret = self._sub_prep(mosaic, self, 'axes',
23912391
width_ratios=width_ratios, height_ratios=height_ratios,
23922392
empty_sentinel=empty_sentinel, subthing_kw=subplot_kw,
23932393
per_subthing_kw=per_subplot_kw, gridspec_kw=gridspec_kw)
2394+
2395+
ax0 = next(iter(ret.values()))
2396+
for ax in ret.values():
2397+
if sharex:
2398+
ax.sharex(ax0)
2399+
ax._label_outer_xaxis(skip_non_rectangular_axes=True)
2400+
if sharey:
2401+
ax.sharey(ax0)
2402+
ax._label_outer_yaxis(skip_non_rectangular_axes=True)
2403+
23942404
return ret
23952405

23962406
def _set_artist_props(self, a):

0 commit comments

Comments
 (0)