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