FIX: removing colorbar's axes also removes colorbar#31555
Open
rcomer wants to merge 1 commit intomatplotlib:mainfrom
Open
FIX: removing colorbar's axes also removes colorbar#31555rcomer wants to merge 1 commit intomatplotlib:mainfrom
rcomer wants to merge 1 commit intomatplotlib:mainfrom
Conversation
3 tasks
timhoffm
reviewed
Apr 23, 2026
timhoffm
reviewed
Apr 23, 2026
Comment on lines
+439
to
+441
| # Override axes' remove method to properly remove the colorbar | ||
| self._ax_remove = self.ax._remove_method | ||
| self.ax._remove_method = functools.partial(_remove_cbar_axes, cbar=self) |
Member
There was a problem hiding this comment.
Suggested change
| # Override axes' remove method to properly remove the colorbar | |
| self._ax_remove = self.ax._remove_method | |
| self.ax._remove_method = functools.partial(_remove_cbar_axes, cbar=self) | |
| # Ensure proper cleanup also when `cbar.ax.remove()` is called. We ensure | |
| # this by overriding the Axes' remove method, so that `cbar.ax.remove()` | |
| # actually calls`cbar.remove()`. In turn, we store the original Axes' remove | |
| # method in `_ax_remove`, which `cbar.remove()` will eventually call to | |
| # clean up the Axes itself. | |
| self._ax_remove = self.ax._remove_method | |
| self.ax._remove_method = lambda self_: self.remove() |
This warrants a bit more explanation - at least I had some trouble understanding how this works. Also, You can get away with a lambda and don't need an extra _remove_cbar_axes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
Closes #31330
Replace the colorbar axes's remove method so it calls the colorbar's own remove method. The code from the issue now gives me a plot. I also thought about having the axes remove method raise an exception with a message to use the colorbar's method instead. However, I think either way we'd need to replace the method and it's no bigger change to support it than to make it raise.
This is an alternative solution to #31549, which was opened first.
AI Disclosure
None
PR checklist