@@ -1785,11 +1785,12 @@ def _identify_keys_and_nested(layout):
1785
1785
1786
1786
Returns
1787
1787
-------
1788
- unique_ids : set
1788
+ unique_ids : tuple
1789
1789
The unique non-sub layout entries in this layout
1790
1790
nested : dict[tuple[int, int]], 2D object array
1791
1791
"""
1792
- unique_ids = set ()
1792
+ # make sure we preserve the user supplied order
1793
+ unique_ids = cbook ._OrderedSet ()
1793
1794
nested = {}
1794
1795
for j , row in enumerate (layout ):
1795
1796
for k , v in enumerate (row ):
@@ -1800,7 +1801,7 @@ def _identify_keys_and_nested(layout):
1800
1801
else :
1801
1802
unique_ids .add (v )
1802
1803
1803
- return unique_ids , nested
1804
+ return tuple ( unique_ids ) , nested
1804
1805
1805
1806
def _do_layout (gs , layout , unique_ids , nested ):
1806
1807
"""
@@ -1811,7 +1812,7 @@ def _do_layout(gs, layout, unique_ids, nested):
1811
1812
gs : GridSpec
1812
1813
layout : 2D object array
1813
1814
The input converted to a 2D numpy array for this level.
1814
- unique_ids : set
1815
+ unique_ids : tuple
1815
1816
The identified scalar labels at this level of nesting.
1816
1817
nested : dict[tuple[int, int]], 2D object array
1817
1818
The identified nested layouts, if any.
@@ -1824,6 +1825,8 @@ def _do_layout(gs, layout, unique_ids, nested):
1824
1825
rows , cols = layout .shape
1825
1826
output = dict ()
1826
1827
1828
+ this_level = dict ()
1829
+
1827
1830
# create the Axes at this level of nesting
1828
1831
for name in unique_ids :
1829
1832
indx = np .argwhere (layout == name )
@@ -1836,26 +1839,39 @@ def _do_layout(gs, layout, unique_ids, nested):
1836
1839
f"While trying to layout\n { layout !r} \n "
1837
1840
f"we found that the label { name !r} specifies a "
1838
1841
"non-rectangular or non-contiguous area." )
1842
+ this_level [(start_row , start_col )] = (name , slc , 'axes' )
1839
1843
1840
- ax = self .add_subplot (
1841
- gs [slc ], ** {'label' : str (name ), ** subplot_kw }
1842
- )
1843
- output [name ] = ax
1844
-
1845
- # do any sub-layouts
1846
1844
for (j , k ), nested_layout in nested .items ():
1847
- rows , cols = nested_layout .shape
1848
- nested_output = _do_layout (
1849
- gs [j , k ].subgridspec (rows , cols , ** gridspec_kw ),
1850
- nested_layout ,
1851
- * _identify_keys_and_nested (nested_layout )
1852
- )
1853
- overlap = set (output ) & set (nested_output )
1854
- if overlap :
1855
- raise ValueError (f"There are duplicate keys { overlap } "
1856
- f"between the outer layout\n { layout !r} \n "
1857
- f"and the nested layout\n { nested_layout } " )
1858
- output .update (nested_output )
1845
+ this_level [(j , k )] = (None , nested_layout , 'nested' )
1846
+
1847
+ for key in sorted (this_level ):
1848
+ name , arg , method = this_level [key ]
1849
+ if method == 'axes' :
1850
+ slc = arg
1851
+ if name in output :
1852
+ raise ValueError (f"There are duplicate keys { name } "
1853
+ f"in the layout\n { layout !r} " )
1854
+ ax = self .add_subplot (
1855
+ gs [slc ], ** {'label' : str (name ), ** subplot_kw }
1856
+ )
1857
+ output [name ] = ax
1858
+ elif method == 'nested' :
1859
+ nested_layout = arg
1860
+ j , k = key
1861
+ rows , cols = nested_layout .shape
1862
+ nested_output = _do_layout (
1863
+ gs [j , k ].subgridspec (rows , cols , ** gridspec_kw ),
1864
+ nested_layout ,
1865
+ * _identify_keys_and_nested (nested_layout )
1866
+ )
1867
+ overlap = set (output ) & set (nested_output )
1868
+ if overlap :
1869
+ raise ValueError (
1870
+ f"There are duplicate keys { overlap } "
1871
+ f"between the outer layout\n { layout !r} \n "
1872
+ f"and the nested layout\n { nested_layout } "
1873
+ )
1874
+ output .update (nested_output )
1859
1875
return output
1860
1876
1861
1877
layout = _make_array (layout )
0 commit comments