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

Skip to content

Adding cla and remove to ColorbarAxes #20323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ def _set_inner_bounds(self, bounds):
self.inner_ax._axes_locator = _TransformedBoundsLocator(
bounds, self.outer_ax.transAxes)

def cla(self):
"""Clear the axes."""
self.inner_ax.cla()
self.outer_ax.cla()

def remove(self):
"""Remove the axes."""
self.inner_ax.remove()
self.outer_ax.remove()


class _ColorbarSpine(mspines.Spine):
def __init__(self, axes):
Expand Down Expand Up @@ -874,8 +884,7 @@ def set_alpha(self, alpha):

def remove(self):
"""Remove this colorbar from the figure."""
self.ax.inner_ax.remove()
self.ax.outer_ax.remove()
self.ax.remove()

def _ticker(self, locator, formatter):
"""
Expand Down
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,17 @@ def test_inset_colorbar_layout():
np.testing.assert_allclose(cb.ax.get_position().bounds,
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
assert cb.ax.outer_ax in ax.child_axes


@check_figures_equal(extensions=["png"])
def test_colorbar_reuse_axes(fig_ref, fig_test):
ax = fig_ref.add_subplot()
pc = ax.imshow(np.arange(100).reshape(10, 10))
cb = fig_ref.colorbar(pc)

ax = fig_test.add_subplot()
pc = ax.imshow(np.arange(100).reshape(10, 10))
cb = fig_test.colorbar(pc)
# Clear and re-use the same colorbar axes
cb.ax.cla()
cb = fig_test.colorbar(pc, cax=cb.ax)