@@ -116,6 +116,7 @@ class AxesWidget(Widget):
116116 def __init__ (self , ax ):
117117 self .ax = ax
118118 self ._cids = []
119+ self ._blit_background_id = None
119120
120121 canvas = property (
121122 lambda self : getattr (self .ax .get_figure (root = True ), 'canvas' , None )
@@ -150,6 +151,26 @@ def ignore(self, event):
150151 # docstring inherited
151152 return super ().ignore (event ) or self .canvas is None
152153
154+ def _save_blit_background (self , background ):
155+ """
156+ Save a blit background.
157+
158+ The background is stored on the canvas in a uniquely identifiable way.
159+ It should be read back via `.load_blit_background`. Be prepared that
160+ some events may invalidate the background, in which case
161+ `.load_blit_background` will return None.
162+
163+ This currently allows at most one background per widget, which is
164+ good enough for all existing widgets.
165+ """
166+ if self ._blit_background_id is None :
167+ self ._blit_background_id = self .canvas ._get_blit_background_id ()
168+ self .canvas ._blit_backgrounds [self ._blit_background_id ] = background
169+
170+ def _load_blit_background (self ):
171+ """Load a blit background; may be None at any time."""
172+ return self .canvas ._blit_backgrounds .get (self ._blit_background_id )
173+
153174
154175class Button (AxesWidget ):
155176 """
@@ -1058,7 +1079,6 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,
10581079 actives = [False ] * len (labels )
10591080
10601081 self ._useblit = useblit and self .canvas .supports_blit
1061- self ._background = None
10621082
10631083 ys = np .linspace (1 , 0 , len (labels )+ 2 )[1 :- 1 ]
10641084
@@ -1105,7 +1125,7 @@ def _clear(self, event):
11051125 """Internal event handler to clear the buttons."""
11061126 if self .ignore (event ) or self .canvas .is_saving ():
11071127 return
1108- self ._background = self .canvas .copy_from_bbox (self .ax .bbox )
1128+ self .save_blit_background ( self .canvas .copy_from_bbox (self .ax .bbox ) )
11091129 self .ax .draw_artist (self ._checks )
11101130
11111131 def _clicked (self , event ):
@@ -1210,8 +1230,9 @@ def set_active(self, index, state=None):
12101230
12111231 if self .drawon :
12121232 if self ._useblit :
1213- if self ._background is not None :
1214- self .canvas .restore_region (self ._background )
1233+ background = self .load_blit_background ()
1234+ if background is not None :
1235+ self .canvas .restore_region (background )
12151236 self .ax .draw_artist (self ._checks )
12161237 self .canvas .blit (self .ax .bbox )
12171238 else :
@@ -1645,7 +1666,6 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
16451666 ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
16461667
16471668 self ._useblit = useblit and self .canvas .supports_blit
1648- self ._background = None
16491669
16501670 label_props = _expand_text_props (label_props )
16511671 self .labels = [
@@ -1687,7 +1707,7 @@ def _clear(self, event):
16871707 """Internal event handler to clear the buttons."""
16881708 if self .ignore (event ) or self .canvas .is_saving ():
16891709 return
1690- self ._background = self .canvas .copy_from_bbox (self .ax .bbox )
1710+ self .save_blit_background ( self .canvas .copy_from_bbox (self .ax .bbox ) )
16911711 self .ax .draw_artist (self ._buttons )
16921712
16931713 def _clicked (self , event ):
@@ -1780,8 +1800,9 @@ def set_active(self, index):
17801800
17811801 if self .drawon :
17821802 if self ._useblit :
1783- if self ._background is not None :
1784- self .canvas .restore_region (self ._background )
1803+ background = self .load_blit_background ()
1804+ if background is not None :
1805+ self .canvas .restore_region (background )
17851806 self .ax .draw_artist (self ._buttons )
17861807 self .canvas .blit (self .ax .bbox )
17871808 else :
@@ -1937,15 +1958,15 @@ def __init__(self, ax, *, horizOn=True, vertOn=True, useblit=False,
19371958 self .lineh = ax .axhline (ax .get_ybound ()[0 ], visible = False , ** lineprops )
19381959 self .linev = ax .axvline (ax .get_xbound ()[0 ], visible = False , ** lineprops )
19391960
1940- self .background = None
19411961 self .needclear = False
19421962
19431963 def clear (self , event ):
19441964 """Internal event handler to clear the cursor."""
19451965 if self .ignore (event ) or self .canvas .is_saving ():
19461966 return
19471967 if self .useblit :
1948- self .background = self .canvas .copy_from_bbox (self .ax .bbox )
1968+ self .save_blit_background (self .canvas .copy_from_bbox (self .ax .bbox ))
1969+ print ("save" )
19491970
19501971 def onmove (self , event ):
19511972 """Internal event handler to draw the cursor when the mouse moves."""
@@ -1970,8 +1991,11 @@ def onmove(self, event):
19701991 return
19711992 # Redraw.
19721993 if self .useblit :
1973- if self .background is not None :
1974- self .canvas .restore_region (self .background )
1994+ background = self .load_blit_background ()
1995+ if background is not None :
1996+ self .canvas .restore_region (background )
1997+ print ('hit' )
1998+ print ("miss" )
19751999 self .ax .draw_artist (self .linev )
19762000 self .ax .draw_artist (self .lineh )
19772001 self .canvas .blit (self .ax .bbox )
@@ -2132,8 +2156,6 @@ def __init__(self, ax, onselect=None, useblit=False, button=None,
21322156 self ._state_modifier_keys .update (state_modifier_keys or {})
21332157 self ._use_data_coordinates = use_data_coordinates
21342158
2135- self .background = None
2136-
21372159 if isinstance (button , Integral ):
21382160 self .validButtons = [button ]
21392161 else :
@@ -2189,7 +2211,7 @@ def update_background(self, event):
21892211 for artist in artists :
21902212 stack .enter_context (artist ._cm_set (visible = False ))
21912213 self .canvas .draw ()
2192- self .background = self .canvas .copy_from_bbox (self .ax .bbox )
2214+ self .save_blit_background ( self .canvas .copy_from_bbox (self .ax .bbox ) )
21932215 if needs_redraw :
21942216 for artist in artists :
21952217 self .ax .draw_artist (artist )
@@ -2236,8 +2258,9 @@ def update(self):
22362258 self .ax .get_figure (root = True )._get_renderer () is None ):
22372259 return
22382260 if self .useblit :
2239- if self .background is not None :
2240- self .canvas .restore_region (self .background )
2261+ background = self .load_blit_background ()
2262+ if background is not None :
2263+ self .canvas .restore_region (background )
22412264 else :
22422265 self .update_background (None )
22432266 # We need to draw all artists, which are not included in the
0 commit comments