@@ -1896,7 +1896,7 @@ def _normalize_grid_string(layout):
1896
1896
layout = inspect .cleandoc (layout )
1897
1897
return [list (ln ) for ln in layout .strip ('\n ' ).split ('\n ' )]
1898
1898
1899
- def _sub_prep (self , mosaic , sub_add_func , * , sharex = False , sharey = False ,
1899
+ def _sub_prep (self , mosaic , parent , mode = 'axes' , * ,
1900
1900
width_ratios = None , height_ratios = None ,
1901
1901
empty_sentinel = '.' ,
1902
1902
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,
1969
1969
# special-case string input
1970
1970
if isinstance (mosaic , str ):
1971
1971
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 ()
1974
1974
}
1975
1975
1976
1976
per_subthing_kw = self ._check_duplication_and_flatten_kwargs (per_subthing_kw )
1977
1977
1978
- # Only accept strict bools to allow a possible future API expansion.
1979
- _api .check_isinstance (bool , sharex = sharex , sharey = sharey )
1980
-
1981
1978
def _make_array (inp ):
1982
1979
"""
1983
1980
Convert input into 2D array
@@ -2047,7 +2044,7 @@ def _identify_keys_and_nested(mosaic):
2047
2044
2048
2045
return tuple (unique_ids ), nested
2049
2046
2050
- def _do_layout (gs , mosaic , unique_ids , nested , sub_add_func ):
2047
+ def _do_layout (gs , mosaic , unique_ids , nested , parent , mode ):
2051
2048
"""
2052
2049
Recursively do the mosaic.
2053
2050
@@ -2080,13 +2077,6 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
2080
2077
# nested mosaic) at this level
2081
2078
this_level = dict ()
2082
2079
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
-
2090
2080
# go through the unique keys,
2091
2081
for name in unique_ids :
2092
2082
# sort out where each axes starts/ends
@@ -2121,6 +2111,10 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
2121
2111
if name in output :
2122
2112
raise ValueError (f"There are duplicate keys { name } "
2123
2113
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
2124
2118
ax = sub_add_func (
2125
2119
gs [slc ], ** {
2126
2120
'label' : str (name ),
@@ -2135,12 +2129,18 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
2135
2129
j , k = key
2136
2130
# recursively add the nested mosaic
2137
2131
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
2138
2138
nested_output = _do_layout (
2139
2139
gs [j , k ].subgridspec (rows , cols ),
2140
2140
nested_mosaic ,
2141
2141
* _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
2144
2144
)
2145
2145
overlap = set (output ) & set (nested_output )
2146
2146
if overlap :
@@ -2157,22 +2157,17 @@ def _do_layout(gs, mosaic, unique_ids, nested, sub_add_func):
2157
2157
mosaic = _make_array (mosaic )
2158
2158
rows , cols = mosaic .shape
2159
2159
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 ):
2170
2163
raise ValueError (
2171
2164
f"The keys { extra } are in *per_subplot_kw* "
2172
2165
"but not in the mosaic."
2173
2166
)
2174
2167
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 ,
2176
2171
height_ratios = None , empty_sentinel = '.' ,
2177
2172
subfigure_kw = None , per_subfigure_kw = None , gridspec_kw = None ):
2178
2173
"""
@@ -2271,7 +2266,8 @@ def subfigure_mosaic(self, mosaic, *, width_ratios=None,
2271
2266
total layout.
2272
2267
2273
2268
"""
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 ,
2275
2271
height_ratios = height_ratios , empty_sentinel = empty_sentinel ,
2276
2272
subthing_kw = subfigure_kw , per_subthing_kw = per_subfigure_kw ,
2277
2273
gridspec_kw = gridspec_kw )
@@ -2387,10 +2383,24 @@ def subplot_mosaic(self, mosaic, *, sharex=False, sharey=False,
2387
2383
total layout.
2388
2384
2389
2385
"""
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' ,
2391
2391
width_ratios = width_ratios , height_ratios = height_ratios ,
2392
2392
empty_sentinel = empty_sentinel , subthing_kw = subplot_kw ,
2393
2393
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
+
2394
2404
return ret
2395
2405
2396
2406
def _set_artist_props (self , a ):
0 commit comments