@@ -1410,10 +1410,13 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14101410 anchor = kwargs .pop ('anchor' , loc_settings ['anchor' ])
14111411 panchor = kwargs .pop ('panchor' , loc_settings ['panchor' ])
14121412 aspect0 = aspect
1413- # turn parents into a list if it is not already. We do this w/ np
1414- # because `plt.subplots` can return an ndarray and is natural to
1415- # pass to `colorbar`.
1416- parents = np .atleast_1d (parents ).ravel ()
1413+ # turn parents into a list if it is not already. Note we cannot
1414+ # use .flatten or .ravel as these copy the references rather than
1415+ # reuse them, leading to a memory leak
1416+ if isinstance (parents , np .ndarray ):
1417+ parents = [parent for parent in parents .flat ]
1418+ if not isinstance (parents , list ):
1419+ parents = [parents ]
14171420 fig = parents [0 ].get_figure ()
14181421
14191422 pad0 = 0.05 if fig .get_constrained_layout () else loc_settings ['pad' ]
@@ -1461,8 +1464,8 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14611464 # tell the parent it has a colorbar
14621465 a ._colorbars += [cax ]
14631466 cax ._colorbar_info = dict (
1464- location = location ,
14651467 parents = parents ,
1468+ location = location ,
14661469 shrink = shrink ,
14671470 anchor = anchor ,
14681471 panchor = panchor ,
@@ -1586,6 +1589,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
15861589 fraction = fraction ,
15871590 aspect = aspect0 ,
15881591 pad = pad )
1592+
15891593 return cax , kwargs
15901594
15911595
0 commit comments