Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9a0dc2

Browse files
committed
MAINT: Adding cla/remove to the ColorbarAxes class
The cla and remove methods need to be applied to both the inner and outer axes.
1 parent 0b3997e commit b9a0dc2

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/matplotlib/colorbar.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ def _set_inner_bounds(self, bounds):
285285
self.inner_ax._axes_locator = _TransformedBoundsLocator(
286286
bounds, self.outer_ax.transAxes)
287287

288+
def cla(self):
289+
"""Clear the axes."""
290+
self.inner_ax.cla()
291+
self.outer_ax.cla()
292+
293+
def remove(self):
294+
"""Remove the axes."""
295+
self.inner_ax.remove()
296+
self.outer_ax.remove()
297+
288298

289299
class _ColorbarSpine(mspines.Spine):
290300
def __init__(self, axes):
@@ -874,8 +884,7 @@ def set_alpha(self, alpha):
874884

875885
def remove(self):
876886
"""Remove this colorbar from the figure."""
877-
self.ax.inner_ax.remove()
878-
self.ax.outer_ax.remove()
887+
self.ax.remove()
879888

880889
def _ticker(self, locator, formatter):
881890
"""

lib/matplotlib/tests/test_colorbar.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,17 @@ def test_inset_colorbar_layout():
771771
np.testing.assert_allclose(cb.ax.get_position().bounds,
772772
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
773773
assert cb.ax.outer_ax in ax.child_axes
774+
775+
776+
@check_figures_equal(extensions=["png"])
777+
def test_colorbar_reuse_axes(fig_ref, fig_test):
778+
ax = fig_ref.add_subplot()
779+
pc = ax.imshow(np.arange(100).reshape(10, 10))
780+
cb = fig_ref.colorbar(pc)
781+
782+
ax = fig_test.add_subplot()
783+
pc = ax.imshow(np.arange(100).reshape(10, 10))
784+
cb = fig_test.colorbar(pc)
785+
# Clear and re-use the same colorbar axes
786+
cb.ax.cla()
787+
cb = fig_test.colorbar(pc, cax=cb.ax)

0 commit comments

Comments
 (0)