@@ -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