10000 Add blitting support to the CheckButtons widget · matplotlib/matplotlib@54e497c · GitHub
[go: up one dir, main page]

Skip to content

Commit 54e497c

Browse files
committed
Add blitting support to the CheckButtons widget
1 parent aae92cf commit 54e497c

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

lib/matplotlib/widgets.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,7 @@ class CheckButtons(AxesWidget):
979979
----------
980980
ax : `~matplotlib.axes.Axes`
981981
The parent Axes for the widget.
982+
982983
labels : list of `.Text`
983984
984985
rectangles : list of `.Rectangle`
@@ -988,21 +989,24 @@ class CheckButtons(AxesWidget):
988989
each box, but have ``set_visible(False)`` when its box is not checked.
989990
"""
990991

991-
def __init__(self, ax, labels, actives=None):
992+
def __init__(self, ax, labels, actives=None, useblit=False):
992993
"""
993994
Add check buttons to `matplotlib.axes.Axes` instance *ax*.
994995
995996
Parameters
996997
----------
997998
ax : `~matplotlib.axes.Axes`
998999
The parent Axes for the widget.
999-
10001000
labels : list of str
10011001
The labels of the check buttons.
1002-
10031002
actives : list of bool, optional
10041003
The initial check states of the buttons. The list must have the
10051004
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
10061010
"""
10071011
super().__init__(ax)
10081012

@@ -1026,8 +1030,13 @@ def __init__(self, ax, labels, actives=None):
10261030
self.lines = []
10271031
self.rectangles = []
10281032

1033+
self._useblit = useblit and self.canvas.supports_blit
1034+
self._background = None
1035+
10291036
lineparams = {'color': 'k', 'linewidth': 1.25,
10301037
'transform': ax.transAxes, 'solid_capstyle': 'butt'}
1038+
if self._useblit:
1039+
lineparams['animated'] = True
10311040
for y, label, active in zip(ys, labels, actives):
10321041
t = ax.text(0.25, y, label, transform=ax.transAxes,
10331042
horizontalalignment='left',
@@ -1052,9 +1061,20 @@ def __init__(self, ax, labels, actives=None):
10521061
ax.add_line(l2)
10531062

10541063
self.connect_event('button_press_event', self._clicked)
1064+
if self._useblit:
1065+
self.connect_event('draw_event', self._clear)
10551066

10561067
self._observers = cbook.CallbackRegistry(signals=["clicked"])
10571068

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+
10581078
def _clicked(self, event):
10591079
if self.ignore(event) or event.button != 1 or event.inaxes != self.ax:
10601080
return
@@ -1088,7 +1108,15 @@ def set_active(self, index):
10881108
l2.set_visible(not l2.get_visible())
10891109

10901110
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()
10921120

10931121
if self.eventson:
10941122
self._observers.process('clicked', self.labels[index].get_text())

0 commit comments

Comments
 (0)
0