Skip to content

Commit a22b676

Browse files
authored
Merge pull request #21030 from meeseeksmachine/auto-backport-of-pr-20980-on-v3.5.x
Backport PR #20980 on branch v3.5.x (FIX: remove colorbar from list of colorbars on axes)
2 parents 271515a + dbb091a commit a22b676

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/matplotlib/colorbar.py

+6
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,12 @@ def remove(self):
992992
If the colorbar was created with ``use_gridspec=True`` the previous
993993
gridspec is restored.
994994
"""
995+
if hasattr(self.ax, '_colorbar_info'):
996+
parents = self.ax._colorbar_info['parents']
997+
for a in parents:
998+
if self.ax in a._colorbars:
999+
a._colorbars.remove(self.ax)
1000+
9951001
self.ax.remove()
9961002

9971003
self.mappable.callbacks.disconnect(self.mappable.colorbar_cid)

lib/matplotlib/tests/test_colorbar.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_colorbar_single_scatter():
221221
ids=['no gridspec', 'with gridspec'])
222222
def test_remove_from_figure(use_gridspec):
223223
"""
224-
Test `remove_from_figure` with the specified ``use_gridspec`` setting
224+
Test `remove` with the specified ``use_gridspec`` setting
225225
"""
226226
fig, ax = plt.subplots()
227227
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
@@ -235,6 +235,23 @@ def test_remove_from_figure(use_gridspec):
235235
assert (pre_position.get_points() == post_position.get_points()).all()
236236

237237

238+
def test_remove_from_figure_cl():
239+
"""
240+
Test `remove` with constrained_layout
241+
"""
242+
fig, ax = plt.subplots(constrained_layout=True)
243+
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
244+
sc.set_array(np.array([5, 6]))
245+
fig.draw_without_rendering()
246+
pre_position = ax.get_position()
247+
cb = fig.colorbar(sc)
248+
cb.remove()
249+
fig.draw_without_rendering()
250+
post_position = ax.get_position()
251+
np.testing.assert_allclose(pre_position.get_points(),
252+
post_position.get_points())
253+
254+
238255
def test_colorbarbase():
239256
# smoke test from #3805
240257
ax = plt.gca()

0 commit comments

Comments
 (0)