8000 Add blitting support to the RadioButtons widget · matplotlib/matplotlib@aae92cf · GitHub
[go: up one dir, main page]

Skip to content

Commit aae92cf

Browse files
committed
Add blitting support to the RadioButtons widget
1 parent aa6c07c commit aae92cf

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

lib/matplotlib/widgets.py

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

1401-
def __init__(self, ax, labels, active=0, activecolor='blue'):
1401+
def __init__(self, ax, labels, active=0, activecolor='blue',
1402+
useblit=False):
14021403
"""
14031404
Add radio buttons to an `~.axes.Axes`.
14041405
@@ -1412,6 +1413,11 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
14121413
The index of the initially selected button.
14131414
activecolor : color
14141415
The color of the selected button.
1416+
useblit : bool, default: False
1417+
Use blitting for faster drawing if supported by the backend.
1418+
See the tutorial :doc:`/tutorials/advanced/blitting` for details.
1419+
1420+
.. versionadded:: 3.7
14151421
"""
14161422
super().__init__(ax)
14171423
self.activecolor = activecolor
@@ -1424,19 +1430,35 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
14241430
ys = np.linspace(1, 0, len(labels) + 2)[1:-1]
14251431
10000 text_size = mpl.rcParams["font.size"] / 2
14261432

1433+
self._useblit = useblit and self.canvas.supports_blit
1434+
self._background = None
1435+
14271436
self.labels = [
14281437
ax.text(0.25, y, label, transform=ax.transAxes,
14291438
horizontalalignment="left", verticalalignment="center")
14301439
for y, label in zip(ys, labels)]
14311440
self._buttons = ax.scatter(
14321441
[.15] * len(ys), ys, transform=ax.transAxes, s=text_size**2,
14331442
c=[activecolor if i == active else "none" for i in range(len(ys))],
1434-
edgecolor="black")
1443+
edgecolor="black", animated=self._useblit)
14351444

14361445
self.connect_event('button_press_event', self._clicked)
1446+
if self._useblit:
1447+
self.connect_event('draw_event', self._clear)
14371448

14381449
self._observers = cbook.CallbackRegistry(signals=["clicked"])
14391450

1451+
def _clear(self, event):
1452+
"""Internal event handler to clear the buttons."""
1453+
if self.ignore(event):
1454+
return
1455+
self._background = self.canvas.copy_from_bbox(self.ax.bbox)
1456+
if hasattr(self, "_circles"): # Remove once circles is removed.
1457+
for circle in self._circles:
1458+
self.ax.draw_artist(circle)
1459+
else:
1460+
self.ax.draw_artist(self._buttons)
1461+
14401462
def _clicked(self, event):
14411463
if self.ignore(event) or event.button != 1 or event.inaxes != self.ax:
14421464
return
@@ -1470,11 +1492,23 @@ def set_active(self, index):
14701492
self.value_selected = self.labels[index].get_text()
14711493
self._buttons.get_facecolor()[:] = colors.to_rgba("none")
14721494
self._buttons.get_facecolor()[index] = colors.to_rgba(self.activecolor)
1495+
if self._background is not None:
1496+
self.canvas.restore_region(self._background)
14731497
if hasattr(self, "_circles"): # Remove once circles is removed.
14741498
for i, p in enumerate(self._circles):
14751499
p.set_facecolor(self.activecolor if i == index else "none")
1500+
if self.drawon and self._useblit:
1501+
self.ax.draw_artist(p)
1502+
else:
1503+
if self.drawon and self._useblit:
1504+
self.ax.draw_artist(self._buttons)
1505+
14761506
if self.drawon:
1477-
self.ax.figure.canvas.draw()
1507+
if self._useblit:
1508+
self.canvas.blit(self.ax.bbox)
1509+
else:
1510+
self.canvas.draw()
1511+
14781512
if self.eventson:
14791513
self._observers.process('clicked', self.labels[index].get_text())
14801514

@@ -1498,7 +1532,8 @@ def circles(self):
14981532
circles = self._circles = [
14991533
Circle(xy=self._buttons.get_offsets()[i], edgecolor="black",
15001534
facecolor=self._buttons.get_facecolor()[i],
1501-
radius=radius, transform=self.ax.transAxes)
1535+
radius=radius, transform=self.ax.transAxes,
1536+
animated=self._useblit)
15021537
for i in range(len(self.labels))]
15031538
self._buttons.set_visible(False)
15041539
for circle in circles:

0 commit comments

Comments
 (0)
0