Skip to content

Commit 3466dd7

Browse files
authored
Merge pull request #22106 from tacaswell/auto-backport-of-pr-22089-on-v3.5.x
Backport PR #22089: FIX: squash memory leak in colorbar
2 parents b5d83e8 + a093725 commit 3466dd7

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/matplotlib/colorbar.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,10 +1403,13 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14031403
anchor = kw.pop('anchor', loc_settings['anchor'])
14041404
panchor = kw.pop('panchor', loc_settings['panchor'])
14051405
aspect0 = aspect
1406-
# turn parents into a list if it is not already. We do this w/ np
1407-
# because `plt.subplots` can return an ndarray and is natural to
1408-
# pass to `colorbar`.
1409-
parents = np.atleast_1d(parents).ravel()
1406+
# turn parents into a list if it is not already. Note we cannot
1407+
# use .flatten or .ravel as these copy the references rather than
1408+
# reuse them, leading to a memory leak
1409+
if isinstance(parents, np.ndarray):
1410+
parents = list(parents.flat)
1411+
elif not isinstance(parents, list):
1412+
parents = [parents]
14101413
fig = parents[0].get_figure()
14111414

14121415
pad0 = 0.05 if fig.get_constrained_layout() else loc_settings['pad']
@@ -1454,8 +1457,8 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14541457
# tell the parent it has a colorbar
14551458
a._colorbars += [cax]
14561459
cax._colorbar_info = dict(
1457-
location=location,
14581460
parents=parents,
1461+
location=location,
14591462
shrink=shrink,
14601463
anchor=anchor,
14611464
panchor=panchor,

0 commit comments

Comments
 (0)