Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 664520d

Browse files
committed
Add blitting support to the CheckButtons widget
1 parent aaa9edb commit 664520d

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

lib/matplotlib/widgets.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ class CheckButtons(AxesWidget):
977977
----------
978978
ax : `~matplotlib.axes.Axes`
979979
The parent Axes for the widget.
980+
980981
labels : list of `.Text`
981982
982983
rectangles : list of `.Rectangle`
@@ -986,21 +987,22 @@ class CheckButtons(AxesWidget):
986987
each box, but have ``set_visible(False)`` when its box is not checked.
987988
"""
988989

989-
def __init__(self, ax, labels, actives=None):
990+
def __init__(self, ax, labels, actives=None, useblit=False):
990991
"""
991992
Add check buttons to `matplotlib.axes.Axes` instance *ax*.
992993
993994
Parameters
994995
----------
995996
ax : `~matplotlib.axes.Axes`
996997
The parent Axes for the widget.
997-
998998
labels : list of str
999999
The labels of the check buttons.
1000-
10011000
actives : list of bool, optional
10021001
The initial check states of the buttons. The list must have the
10031002
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.
10041006
"""
10051007
super().__init__(ax)
10061008

@@ -1024,8 +1026,13 @@ def __init__(self, ax, labels, actives=None):
10241026
self.lines = []
10251027
self.rectangles = []
10261028

1029+
self._useblit = useblit and self.canvas.supports_blit
1030+
self._background = None
1031+
10271032
lineparams = {'color': 'k', 'linewidth': 1.25,
10281033
'transform': ax.transAxes, 'solid_capstyle': 'butt'}
1034+
if self._useblit:
1035+
lineparams['animated'] = True
10291036
for y, label, active in zip(ys, labels, actives):
10301037
t = ax.text(0.25, y, label, transform=ax.transAxes,
10311038
horizontalalignment='left',
@@ -1050,9 +1057,20 @@ def __init__(self, ax, labels, actives=None):
10501057
ax.add_line(l2)
10511058

10521059
self.connect_event('button_press_event', self._clicked)
1060+
if self._useblit:
1061+
self.connect_event('draw_event', self._clear)
10531062

10541063
self._observers = cbook.CallbackRegistry(signals=["clicked"])
10551064

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+
10561074
def _clicked(self, event):
10571075
if self.ignore(event) or event.button != 1 or event.inaxes != self.ax:
10581076
return
@@ -1086,7 +1104,15 @@ def set_active(self, index):
10861104
l2.set_visible(not l2.get_visible())
10871105

10881106
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()
10901116

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

0 commit comments

Comments
 (0)