Skip to content

Commit 75b05de

Browse files
committed
Add blitting support to the RadioButtons widget
1 parent c9d9ff7 commit 75b05de

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

lib/matplotlib/widgets.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,8 @@ class RadioButtons(AxesWidget):
14061406
The label text of the currently selected button.
14071407
"""
14081408

1409-
def __init__(self, ax, labels, active=0, activecolor='blue'):
1409+
def __init__(self, ax, labels, active=0, activecolor='blue',
1410+
useblit=False):
14101411
"""
14111412
Add radio buttons to an `~.axes.Axes`.
14121413
@@ -1420,6 +1421,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
14201421
The index of the initially selected button.
14211422
activecolor : color
14221423
The color of the selected button.
1424+
useblit : bool, default: False
1425+
Use blitting for faster drawing if supported by the backend.
1426+
See the tutorial :doc:`/tutorials/advanced/blitting` for details.
14231427
"""
14241428
super().__init__(ax)
14251429
self.activecolor = activecolor
@@ -1438,6 +1442,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
14381442
# default to hard-coded value if the radius becomes too large
14391443
circle_radius = min(circle_radius, 0.05)
14401444

1445+
self._useblit = useblit and self.canvas.supports_blit
1446+
animated_style = {'animated': True} if self._useblit else {}
1447+
14411448
self.labels = []
14421449
self.circles = []
14431450
for y, label in zip(ys, labels):
@@ -1452,17 +1459,27 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
14521459
facecolor = axcolor
14531460

14541461
p = Circle(xy=(0.15, y), radius=circle_radius, edgecolor='black',
1455-
facecolor=facecolor, transform=ax.transAxes)
1462+
facecolor=facecolor, transform=ax.transAxes,
1463+
**animated_style)
14561464

14571465
self.labels.append(t)
14581466
self.circles.append(p)
14591467
ax.add_patch(p)
14601468
cnt += 1
14611469

14621470
self.connect_event('button_press_event', self._clicked)
1471+
if self._useblit:
1472+
self.connect_event('draw_event', self._clear)
14631473

14641474
self._observers = cbook.CallbackRegistry(signals=["clicked"])
14651475

1476+
def _clear(self, event):
1477+
"""Internal event handler to clear the buttons."""
1478+
if self.ignore(event):
1479+
return
1480+
for circle in self.circles:
1481+
self.ax.draw_artist(circle)
1482+
14661483
def _clicked(self, event):
14671484
if self.ignore(event) or event.button != 1 or event.inaxes != self.ax:
14681485
return
@@ -1493,9 +1510,14 @@ def set_active(self, index):
14931510
else:
14941511
color = self.ax.get_facecolor()
14951512
p.set_facecolor(color)
1513+
if self.drawon and self._useblit:
1514+
self.ax.draw_artist(p)
14961515

14971516
if self.drawon:
1498-
self.ax.figure.canvas.draw()
1517+
if self._useblit:
1518+
self.canvas.blit(self.ax.bbox)
1519+
else:
1520+
self.canvas.draw()
14991521

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

0 commit comments

Comments
 (0)