@@ -1825,29 +1825,47 @@ def _do_layout(gs, layout, unique_ids, nested):
1825
1825
rows , cols = layout .shape
1826
1826
output = dict ()
1827
1827
1828
+ # we need to merge together the Axes at this level and the axes
1829
+ # in the (recursively) nested sub-layouts so that we can add
1830
+ # them to the figure in the "natural" order if you were to
1831
+ # ravel in c-order all of the Axes that will be created
1832
+ #
1833
+ # This will stash the upper left index of each object (axes or
1834
+ # nested layout) at this level
1828
1835
this_level = dict ()
1829
1836
1830
- # create the Axes at this level of nesting
1837
+ # go through the unique keys,
1831
1838
for name in unique_ids :
1839
+ # sort out where each axes starts/ends
1832
1840
indx = np .argwhere (layout == name )
1833
1841
start_row , start_col = np .min (indx , axis = 0 )
1834
1842
end_row , end_col = np .max (indx , axis = 0 ) + 1
1843
+ # and construct the slice object
1835
1844
slc = (slice (start_row , end_row ), slice (start_col , end_col ))
1836
-
1845
+ # some light error checking
1837
1846
if (layout [slc ] != name ).any ():
1838
1847
raise ValueError (
1839
1848
f"While trying to layout\n { layout !r} \n "
1840
1849
f"we found that the label { name !r} specifies a "
1841
1850
"non-rectangular or non-contiguous area." )
1851
+ # and stash this slice for later
1842
1852
this_level [(start_row , start_col )] = (name , slc , 'axes' )
1843
1853
1854
+ # do the same thing for the nested layouts (simpler because these
1855
+ # can not be spans yet!)
1844
1856
for (j , k ), nested_layout in nested .items ():
1845
1857
this_level [(j , k )] = (None , nested_layout , 'nested' )
1846
1858
1859
+ # now go through the things in this level and add them
1860
+ # in order left-to-right top-to-bottom
1847
1861
for key in sorted (this_level ):
1848
1862
name , arg , method = this_level [key ]
1863
+ # we are doing some hokey function dispatch here based
1864
+ # on the 'method' string stashed above to sort out if this
1865
+ # element is an axes or a nested layout.
1849
1866
if method == 'axes' :
1850
1867
slc = arg
1868
+ # add a single axes
1851
1869
if name in output :
1852
1870
raise ValueError (f"There are duplicate keys { name } "
1853
1871
f"in the layout\n { layout !r} " )
@@ -1858,6 +1876,7 @@ def _do_layout(gs, layout, unique_ids, nested):
1858
1876
elif method == 'nested' :
1859
1877
nested_layout = arg
1860
1878
j , k = key
1879
+ # recursively add the nested layout
1861
1880
rows , cols = nested_layout .shape
1862
1881
nested_output = _do_layout (
1863
1882
gs [j , k ].subgridspec (rows , cols , ** gridspec_kw ),
@@ -1872,6 +1891,8 @@ def _do_layout(gs, layout, unique_ids, nested):
1872
1891
f"and the nested layout\n { nested_layout } "
1873
1892
)
1874
1893
output .update (nested_output )
1894
+ else :
1895
+ raise RuntimeError ("This should never happen" )
1875
1896
return output
1876
1897
1877
1898
layout = _make_array (layout )
0 commit comments