@@ -977,6 +977,7 @@ class CheckButtons(AxesWidget):
977
977
----------
978
978
ax : `~matplotlib.axes.Axes`
979
979
The parent Axes for the widget.
980
+
980
981
labels : list of `.Text`
981
982
982
983
rectangles : list of `.Rectangle`
@@ -986,21 +987,22 @@ class CheckButtons(AxesWidget):
986
987
each box, but have ``set_visible(False)`` when its box is not checked.
987
988
"""
988
989
989
- def __init__ (self , ax , labels , actives = None ):
990
+ def __init__ (self , ax , labels , actives = None , useblit = False ):
990
991
"""
991
992
Add check buttons to `matplotlib.axes.Axes` instance *ax*.
992
993
993
994
Parameters
994
995
----------
995
996
ax : `~matplotlib.axes.Axes`
996
997
The parent Axes for the widget.
997
-
998
998
labels : list of str
999
999
The labels of the check buttons.
1000
-
1001
1000
actives : list of bool, optional
1002
1001
The initial check states of the buttons. The list must have the
1003
1002
same length as *labels*. If not given, all buttons are unchecked.
1003
+ useblit : bool, default: False
1004
+ Use blitting for faster drawing if supported by the backend.
1005
+ See the tutorial :doc:`/tutorials/advanced/blitting` for details.
1004
1006
"""
1005
1007
super ().__init__ (ax )
1006
1008
@@ -1024,8 +1026,13 @@ def __init__(self, ax, labels, actives=None):
1024
1026
self .lines = []
1025
1027
self .rectangles = []
1026
1028
1029
+ self ._useblit = useblit and self .canvas .supports_blit
1030
+ self ._background = None
1031
+
1027
1032
lineparams = {'color' : 'k' , 'linewidth' : 1.25 ,
1028
1033
'transform' : ax .transAxes , 'solid_capstyle' : 'butt' }
1034
+ if self ._useblit :
1035
+ lineparams ['animated' ] = True
1029
1036
for y , label , active in zip (ys , labels , actives ):
1030
1037
t = ax .text (0.25 , y , label , transform = ax .transAxes ,
1031
1038
horizontalalignment = 'left' ,
@@ -1050,9 +1057,20 @@ def __init__(self, ax, labels, actives=None):
1050
1057
ax .add_line (l2 )
1051
1058
1052
1059
self .connect_event ('button_press_event' , self ._clicked )
1060
+ if self ._useblit :
1061
+ self .connect_event ('draw_event' , self ._clear )
1053
1062
1054
1063
self ._observers = cbook .CallbackRegistry (signals = ["clicked" ])
1055
1064
1065
+ def _clear (self , event ):
1066
+ """Internal event handler to clear the buttons."""
1067
+ if self .ignore (event ):
1068
+ return
1069
+ self ._background = self .canvas .copy_from_bbox (self .ax .bbox )
1070
+ for l1 , l2 in self .lines :
1071
+ self .ax .draw_artist (l1 )
1072
+ self .ax .draw_artist (l2 )
1073
+
1056
1074
def _clicked (self , event ):
1057
1075
if self .ignore (event ) or event .button != 1 or event .inaxes != self .ax :
1058
1076
return
@@ -1086,7 +1104,15 @@ def set_active(self, index):
1086
1104
l2 .set_visible (not l2 .get_visible ())
1087
1105
1088
1106
if self .drawon :
1089
- self .ax .figure .canvas .draw ()
1107
+ if self ._useblit :
1108
+ if self ._background is not None :
1109
+ self .canvas .restore_region (self ._background )
1110
+ for l1 , l2 in self .lines :
1111
+ self .ax .draw_artist (l1 )
1112
+ self .ax .draw_artist (l2 )
1113
+ self .canvas .blit (self .ax .bbox )
1114
+ else :
1115
+ self .canvas .draw ()
1090
1116
1091
1117
if self .eventson :
1092
1118
self ._observers .process ('clicked' , self .labels [index ].get_text ())
0 commit comments