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

Skip to content

Commit e1f9cc9

Browse files
committed
FIX: Add cla() function to colorbar interactivity
This adds logic to remove the colorbar interactivity and replace the axes it is drawn in with the original interactive routines.
1 parent b4ddd97 commit e1f9cc9

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,27 @@ def __init__(self, ax, mappable=None, *, cmap=None,
497497

498498
# Link the Axes and Colorbar for interactive use
499499
self.ax._colorbar = self
500-
for x in ["_get_view", "_set_view", "_set_view_from_bbox",
501-
"drag_pan", "start_pan", "end_pan"]:
502-
setattr(self.ax, x, getattr(self, x))
503500
# Don't navigate on any of these types of mappables
504501
if (isinstance(self.norm, (colors.BoundaryNorm, colors.NoNorm)) or
505502
isinstance(self.mappable, contour.ContourSet)):
506503
self.ax.set_navigate(False)
507504

505+
# These are the functions that set up interactivity on this colorbar
506+
self._interactive_funcs = ["_get_view", "_set_view",
507+
"_set_view_from_bbox", "drag_pan"]
508+
for x in self._interactive_funcs:
509+
setattr(self.ax, x, getattr(self, x))
510+
# Set the cla function to the cbar's method to override it
511+
self.ax.cla = self._cbar_cla
512+
513+
def _cbar_cla(self):
514+
"""Function to clear the interactive colorbar state."""
515+
for x in self._interactive_funcs:
516+
delattr(self.ax, x)
517+
# We now restore the old cla() back and can call it directly
518+
del self.ax.cla
519+
self.ax.cla()
520+
508521
# Also remove ._patch after deprecation elapses.
509522
patch = _api.deprecate_privatize_attribute("3.5", alternative="ax")
510523

0 commit comments

Comments
 (0)