@@ -1406,7 +1406,8 @@ class RadioButtons(AxesWidget):
1406
1406
The label text of the currently selected button.
1407
1407
"""
1408
1408
1409
- def __init__ (self , ax , labels , active = 0 , activecolor = 'blue' ):
1409
+ def __init__ (self , ax , labels , active = 0 , activecolor = 'blue' ,
1410
+ useblit = False ):
1410
1411
"""
1411
1412
Add radio buttons to an `~.axes.Axes`.
1412
1413
@@ -1420,6 +1421,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1420
1421
The index of the initially selected button.
1421
1422
activecolor : color
1422
1423
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.
1423
1427
"""
1424
1428
super ().__init__ (ax )
1425
1429
self .activecolor = activecolor
@@ -1438,6 +1442,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1438
1442
# default to hard-coded value if the radius becomes too large
1439
1443
circle_radius = min (circle_radius , 0.05 )
1440
1444
1445
+ self ._useblit = useblit and self .canvas .supports_blit
1446
+ animated_style = {'animated' : True } if self ._useblit else {}
1447
+
1441
1448
self .labels = []
1442
1449
self .circles = []
1443
1450
for y , label in zip (ys , labels ):
@@ -1452,17 +1459,27 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1452
1459
facecolor = axcolor
1453
1460
1454
1461
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 )
1456
1464
1457
1465
self .labels .append (t )
1458
1466
self .circles .append (p )
1459
1467
ax .add_patch (p )
1460
1468
cnt += 1
1461
1469
1462
1470
self .connect_event ('button_press_event' , self ._clicked )
1471
+ if self ._useblit :
1472
+ self .connect_event ('draw_event' , self ._clear )
1463
1473
1464
1474
self ._observers = cbook .CallbackRegistry (signals = ["clicked" ])
1465
1475
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
+
1466
1483
def _clicked (self , event ):
1467
1484
if self .ignore (event ) or event .button != 1 or event .inaxes != self .ax :
1468
1485
return
@@ -1493,9 +1510,14 @@ def set_active(self, index):
1493
1510
else :
1494
1511
color = self .ax .get_facecolor ()
1495
1512
p .set_facecolor (color )
1513
+ if self .drawon and self ._useblit :
1514
+ self .ax .draw_artist (p )
1496
1515
1497
1516
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 ()
1499
1521
1500
1522
if self .eventson :
1501
1523
self ._observers .process ('clicked' , self .labels [index ].get_text ())
0 commit comments