@@ -1825,29 +1825,47 @@ def _do_layout(gs, layout, unique_ids, nested):
18251825 rows , cols = layout .shape
18261826 output = dict ()
18271827
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
18281835 this_level = dict ()
18291836
1830- # create the Axes at this level of nesting
1837+ # go through the unique keys,
18311838 for name in unique_ids :
1839+ # sort out where each axes starts/ends
18321840 indx = np .argwhere (layout == name )
18331841 start_row , start_col = np .min (indx , axis = 0 )
18341842 end_row , end_col = np .max (indx , axis = 0 ) + 1
1843+ # and construct the slice object
18351844 slc = (slice (start_row , end_row ), slice (start_col , end_col ))
1836-
1845+ # some light error checking
18371846 if (layout [slc ] != name ).any ():
18381847 raise ValueError (
18391848 f"While trying to layout\n { layout !r} \n "
18401849 f"we found that the label { name !r} specifies a "
18411850 "non-rectangular or non-contiguous area." )
1851+ # and stash this slice for later
18421852 this_level [(start_row , start_col )] = (name , slc , 'axes' )
18431853
1854+ # do the same thing for the nested layouts (simpler because these
1855+ # can not be spans yet!)
18441856 for (j , k ), nested_layout in nested .items ():
18451857 this_level [(j , k )] = (None , nested_layout , 'nested' )
18461858
1859+ # now go through the things in this level and add them
1860+ # in order left-to-right top-to-bottom
18471861 for key in sorted (this_level ):
18481862 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.
18491866 if method == 'axes' :
18501867 slc = arg
1868+ # add a single axes
18511869 if name in output :
18521870 raise ValueError (f"There are duplicate keys { name } "
18531871 f"in the layout\n { layout !r} " )
@@ -1858,6 +1876,7 @@ def _do_layout(gs, layout, unique_ids, nested):
18581876 elif method == 'nested' :
18591877 nested_layout = arg
18601878 j , k = key
1879+ # recursively add the nested layout
18611880 rows , cols = nested_layout .shape
18621881 nested_output = _do_layout (
18631882 gs [j , k ].subgridspec (rows , cols , ** gridspec_kw ),
@@ -1872,6 +1891,8 @@ def _do_layout(gs, layout, unique_ids, nested):
18721891 f"and the nested layout\n { nested_layout } "
18731892 )
18741893 output .update (nested_output )
1894+ else :
1895+ raise RuntimeError ("This should never happen" )
18751896 return output
18761897
18771898 layout = _make_array (layout )
0 commit comments