@@ -840,6 +840,14 @@ def _locate(self, x):
840840 def set_alpha (self , alpha ):
841841 self .alpha = alpha
842842
843+ def remove (self ):
844+ """
845+ Remove this colorbar from the figure
846+ """
847+
848+ fig = self .ax .figure
849+ fig .delaxes (self .ax )
850+
843851
844852class Colorbar (ColorbarBase ):
845853 """
@@ -967,6 +975,33 @@ def update_bruteforce(self, mappable):
967975 # be recalculating everything if there was a simple alpha
968976 # change.
969977
978+ def remove (self ):
979+ """
980+ Remove this colorbar from the figure. If the colorbar was created with
981+ ``use_gridspec=True`` then restore the gridspec to its previous value.
982+ """
983+
984+ ColorbarBase .remove (self )
985+ self .mappable .callbacksSM .disconnect (self .mappable .colorbar_cid )
986+ self .mappable .colorbar = None
987+ self .mappable .colorbar_cid = None
988+
989+ try :
990+ ax = self .mappable .axes
991+ except AttributeError :
992+ return
993+
994+ try :
995+ gs = ax .get_subplotspec ().get_gridspec ()
996+ subplotspec = gs .get_topmost_subplotspec ()
997+ except AttributeError :
998+ # use_gridspec was False
999+ pos = ax .get_position (original = True )
1000+ ax .set_position (pos )
1001+ else :
1002+ # use_gridspec was True
1003+ ax .set_subplotspec (subplotspec )
1004+
9701005
9711006@docstring .Substitution (make_axes_kw_doc )
9721007def make_axes (parents , location = None , orientation = None , fraction = 0.15 ,
@@ -1280,7 +1315,8 @@ def colorbar_factory(cax, mappable, **kwargs):
12801315 else :
12811316 cb = Colorbar (cax , mappable , ** kwargs )
12821317
1283- mappable .callbacksSM .connect ('changed' , cb .on_mappable_changed )
1318+ cid = mappable .callbacksSM .connect ('changed' , cb .on_mappable_changed )
12841319 mappable .colorbar = cb
1320+ mappable .colorbar_cid = cid
12851321
12861322 return cb
0 commit comments