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

Skip to content

Commit aaa9edb

Browse files
committed
Add blitting support to the RadioButtons widget
1 parent 7bdce94 commit aaa9edb

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

lib/matplotlib/widgets.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,8 @@ class RadioButtons(AxesWidget):
13961396
The label text of the currently selected button.
13971397
"""
13981398

1399-
def __init__(self, ax, labels, active=0, activecolor='blue'):
1399+
def __init__(self, ax, labels, active=0, activecolor='blue',
1400+
useblit=False):
14001401
"""
14011402
Add radio buttons to an `~.axes.Axes`.
14021403
@@ -1410,6 +1411,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
14101411
The index of the initially selected button.
14111412
activecolor : color
14121413
The color of the selected button.
1414+
useblit : bool, default: False
1415+
Use blitting for faster drawing if supported by the backend.
1416+
See the tutorial :doc:`/tutorials/advanced/blitting` for details.
14131417
"""
14141418
super().__init__(ax)
14151419
self.activecolor = activecolor
@@ -1422,19 +1426,35 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
14221426
ys = np.linspace(1, 0, len(labels) + 2)[1:-1]
14231427
text_size = mpl.rcParams["font.size"] / 2
14241428

1429+
self._useblit = useblit and self.canvas.supports_blit
1430+
self._background = None
1431+
14251432
self.labels = [
14261433
ax.text(0.25, y, label, transform=ax.transAxes,
14271434
horizontalalignment="left", verticalalignment="center")
14281435
for y, label in zip(ys, labels)]
14291436
self._buttons = ax.scatter(
14301437
[.15] * len(ys), ys, transform=ax.transAxes, s=text_size**2,
14311438
c=[activecolor if i == active else "none" for i in range(len(ys))],
1432-
edgecolor="black")
1439+
edgecolor="black", animated=self._useblit)
14331440

14341441
self.connect_event('button_press_event', self._clicked)
1442+
if self._useblit:
1443+
self.connect_event('draw_event', self._clear)
14351444

14361445
self._observers = cbook.CallbackRegistry(signals=["clicked"])
14371446

1447+
def _clear(self, event):
1448+
"""Internal event handler to clear the buttons."""
1449+
if self.ignore(event):
1450+
return
1451+
self._background = self.canvas.copy_from_bbox(self.ax.bbox)
1452+
if hasattr(self, "_circles"): # Remove once circles is removed.
1453+
for circle in self._circles:
1454+
self.ax.draw_artist(circle)
1455+
else:
1456+
self.ax.draw_artist(self._buttons)
1457+
14381458
def _clicked(self, event):
14391459
if self.ignore(event) or event.button != 1 or event.inaxes != self.ax:
14401460
return
@@ -1468,11 +1488,23 @@ def set_active(self, index):
14681488
self.value_selected = self.labels[index].get_text()
14691489
self._buttons.get_facecolor()[:] = colors.to_rgba("none")
14701490
self._buttons.get_facecolor()[index] = colors.to_rgba(self.activecolor)
1491+
if self._background is not None:
1492+
self.canvas.restore_region(self._background)
14711493
if hasattr(self, "_circles"): # Remove once circles is removed.
14721494
for i, p in enumerate(self._circles):
14731495
p.set_facecolor(self.activecolor if i == index else "none")
1496+
if self.drawon and self._useblit:
1497+
self.ax.draw_artist(p)
1498+
else:
1499+
if self.drawon and self._useblit:
1500+
self.ax.draw_artist(self._buttons)
1501+
14741502
if self.drawon:
1475-
self.ax.figure.canvas.draw()
1503+
if self._useblit:
1504+
self.canvas.blit(self.ax.bbox)
1505+
else:
1506+
self.canvas.draw()
1507+
14761508
if self.eventson:
14771509
self._observers.process('clicked', self.labels[index].get_text())
14781510

@@ -1496,7 +1528,8 @@ def circles(self):
14961528
circles = self._circles = [
14971529
Circle(xy=self._buttons.get_offsets()[i], edgecolor="black",
14981530
facecolor=self._buttons.get_facecolor()[i],
1499-
radius=radius, transform=self.ax.transAxes)
1531+
radius=radius, transform=self.ax.transAxes,
1532+
animated=self._useblit)
15001533
for i in range(len(self.labels))]
15011534
self._buttons.set_visible(False)
15021535
for circle in circles:

0 commit comments

Comments
 (0)