@@ -117,6 +117,7 @@ class AxesWidget(Widget):
117117 def __init__ (self , ax ):
118118 self .ax = ax
119119 self ._cids = []
120+ self ._blit_background_id = None
120121
121122 canvas = property (
122123 lambda self : getattr (self .ax .get_figure (root = True ), 'canvas' , None )
@@ -155,6 +156,26 @@ def _set_cursor(self, cursor):
155156 """Update the canvas cursor."""
156157 self .ax .get_figure (root = True ).canvas .set_cursor (cursor )
157158
159+ def _save_blit_background (self , background ):
160+ """
161+ Save a blit background.
162+
163+ The background is stored on the canvas in a uniquely identifiable way.
164+ It should be read back via `._load_blit_background`. Be prepared that
165+ some events may invalidate the background, in which case
166+ `._load_blit_background` will return None.
167+
168+ This currently allows at most one background per widget, which is
169+ good enough for all existing widgets.
170+ """
171+ if self ._blit_background_id is None :
172+ self ._blit_background_id = self .canvas ._get_blit_background_id ()
173+ self .canvas ._blit_backgrounds [self ._blit_background_id ] = background
174+
175+ def _load_blit_background (self ):
176+ """Load a blit background; may be None at any time."""
177+ return self .canvas ._blit_backgrounds .get (self ._blit_background_id )
178+
158179
159180class Button (AxesWidget ):
160181 """
@@ -1063,7 +1084,6 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,
10631084 actives = [False ] * len (labels )
10641085
10651086 self ._useblit = useblit and self .canvas .supports_blit
1066- self ._background = None
10671087
10681088 ys = np .linspace (1 , 0 , len (labels )+ 2 )[1 :- 1 ]
10691089
@@ -1110,7 +1130,7 @@ def _clear(self, event):
11101130 """Internal event handler to clear the buttons."""
11111131 if self .ignore (event ) or self .canvas .is_saving ():
11121132 return
1113- self ._background = self .canvas .copy_from_bbox (self .ax .bbox )
1133+ self ._save_blit_background ( self .canvas .copy_from_bbox (self .ax .bbox ) )
11141134 self .ax .draw_artist (self ._checks )
11151135
11161136 def _clicked (self , event ):
@@ -1215,8 +1235,9 @@ def set_active(self, index, state=None):
12151235
12161236 if self .drawon :
12171237 if self ._useblit :
1218- if self ._background is not None :
1219- self .canvas .restore_region (self ._background )
1238+ background = self ._load_blit_background ()
1239+ if background is not None :
1240+ self .canvas .restore_region (background )
12201241 self .ax .draw_artist (self ._checks )
12211242 self .canvas .blit (self .ax .bbox )
12221243 else :
@@ -1650,7 +1671,6 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
16501671 ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
16511672
16521673 self ._useblit = useblit and self .canvas .supports_blit
1653- self ._background = None
16541674
16551675 label_props = _expand_text_props (label_props )
16561676 self .labels = [
@@ -1692,7 +1712,7 @@ def _clear(self, event):
16921712 """Internal event handler to clear the buttons."""
16931713 if self .ignore (event ) or self .canvas .is_saving ():
16941714 return
1695- self ._background = self .canvas .copy_from_bbox (self .ax .bbox )
1715+ self ._save_blit_background ( self .canvas .copy_from_bbox (self .ax .bbox ) )
16961716 self .ax .draw_artist (self ._buttons )
16971717
16981718 def _clicked (self , event ):
@@ -1785,8 +1805,9 @@ def set_active(self, index):
17851805
17861806 if self .drawon :
17871807 if self ._useblit :
1788- if self ._background is not None :
1789- self .canvas .restore_region (self ._background )
1808+ background = self ._load_blit_background ()
1809+ if background is not None :
1810+ self .canvas .restore_region (background )
17901811 self .ax .draw_artist (self ._buttons )
17911812 self .canvas .blit (self .ax .bbox )
17921813 else :
@@ -1942,15 +1963,14 @@ def __init__(self, ax, *, horizOn=True, vertOn=True, useblit=False,
19421963 self .lineh = ax .axhline (ax .get_ybound ()[0 ], visible = False , ** lineprops )
19431964 self .linev = ax .axvline (ax .get_xbound ()[0 ], visible = False , ** lineprops )
19441965
1945- self .background = None
19461966 self .needclear = False
19471967
19481968 def clear (self , event ):
19491969 """Internal event handler to clear the cursor."""
19501970 if self .ignore (event ) or self .canvas .is_saving ():
19511971 return
19521972 if self .useblit :
1953- self .background = self .canvas .copy_from_bbox (self .ax .bbox )
1973+ self ._save_blit_background ( self .canvas .copy_from_bbox (self .ax .bbox ) )
19541974
19551975 def onmove (self , event ):
19561976 """Internal event handler to draw the cursor when the mouse moves."""
@@ -1975,8 +1995,9 @@ def onmove(self, event):
19751995 return
19761996 # Redraw.
19771997 if self .useblit :
1978- if self .background is not None :
1979- self .canvas .restore_region (self .background )
1998+ background = self ._load_blit_background ()
1999+ if background is not None :
2000+ self .canvas .restore_region (background )
19802001 self .ax .draw_artist (self .linev )
19812002 self .ax .draw_artist (self .lineh )
19822003 self .canvas .blit (self .ax .bbox )
@@ -2137,8 +2158,6 @@ def __init__(self, ax, onselect=None, useblit=False, button=None,
21372158 self ._state_modifier_keys .update (state_modifier_keys or {})
21382159 self ._use_data_coordinates = use_data_coordinates
21392160
2140- self .background = None
2141-
21422161 if isinstance (button , Integral ):
21432162 self .validButtons = [button ]
21442163 else :
@@ -2194,7 +2213,7 @@ def update_background(self, event):
21942213 for artist in artists :
21952214 stack .enter_context (artist ._cm_set (visible = False ))
21962215 self .canvas .draw ()
2197- self .background = self .canvas .copy_from_bbox (self .ax .bbox )
2216+ self ._save_blit_background ( self .canvas .copy_from_bbox (self .ax .bbox ) )
21982217 if needs_redraw :
21992218 for artist in artists :
22002219 self .ax .draw_artist (artist )
@@ -2241,8 +2260,9 @@ def update(self):
22412260 self .ax .get_figure (root = True )._get_renderer () is None ):
22422261 return
22432262 if self .useblit :
2244- if self .background is not None :
2245- self .canvas .restore_region (self .background )
2263+ background = self ._load_blit_background ()
2264+ if background is not None :
2265+ self .canvas .restore_region (background )
22462266 else :
22472267 self .update_background (None )
22482268 # We need to draw all artists, which are not included in the
0 commit comments