@@ -1410,10 +1410,13 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
1410
1410
anchor = kwargs .pop ('anchor' , loc_settings ['anchor' ])
1411
1411
panchor = kwargs .pop ('panchor' , loc_settings ['panchor' ])
1412
1412
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 ]
1417
1420
fig = parents [0 ].get_figure ()
1418
1421
1419
1422
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,
1461
1464
# tell the parent it has a colorbar
1462
1465
a ._colorbars += [cax ]
1463
1466
cax ._colorbar_info = dict (
1464
- location = location ,
1465
1467
parents = parents ,
1468
+ location = location ,
1466
1469
shrink = shrink ,
1467
1470
anchor = anchor ,
1468
1471
panchor = panchor ,
@@ -1586,6 +1589,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
1586
1589
fraction = fraction ,
1587
1590
aspect = aspect0 ,
1588
1591
pad = pad )
1592
+
1589
1593
return cax , kwargs
1590
1594
1591
1595
0 commit comments