@@ -1396,7 +1396,8 @@ class RadioButtons(AxesWidget):
1396
1396
The label text of the currently selected button.
1397
1397
"""
1398
1398
1399
- def __init__ (self , ax , labels , active = 0 , activecolor = 'blue' ):
1399
+ def __init__ (self , ax , labels , active = 0 , activecolor = 'blue' ,
1400
+ useblit = False ):
1400
1401
"""
1401
1402
Add radio buttons to an `~.axes.Axes`.
1402
1403
@@ -1410,6 +1411,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1410
1411
The index of the initially selected button.
1411
1412
activecolor : color
1412
1413
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.
1413
1417
"""
1414
1418
super ().__init__ (ax )
1415
1419
self .activecolor = activecolor
@@ -1422,19 +1426,35 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1422
1426
ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
1423
1427
text_size = mpl .rcParams ["font.size" ] / 2
1424
1428
1429
+ self ._useblit = useblit and self .canvas .supports_blit
1430
+ self ._background = None
1431
+
1425
1432
self .labels = [
1426
1433
ax .text (0.25 , y , label , transform = ax .transAxes ,
1427
1434
horizontalalignment = "left" , verticalalignment = "center" )
1428
1435
for y , label in zip (ys , labels )]
1429
1436
self ._buttons = ax .scatter (
1430
1437
[.15 ] * len (ys ), ys , transform = ax .transAxes , s = text_size ** 2 ,
1431
1438
c = [activecolor if i == active else "none" for i in range (len (ys ))],
1432
- edgecolor = "black" )
1439
+ edgecolor = "black" , animated = self . _useblit )
1433
1440
1434
1441
self .connect_event ('button_press_event' , self ._clicked )
1442
+ if self ._useblit :
1443
+ self .connect_event ('draw_event' , self ._clear )
1435
1444
1436
1445
self ._observers = cbook .CallbackRegistry (signals = ["clicked" ])
1437
1446
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
+
1438
1458
def _clicked (self , event ):
1439
1459
if self .ignore (event ) or event .button != 1 or event .inaxes != self .ax :
1440
1460
return
@@ -1468,11 +1488,23 @@ def set_active(self, index):
1468
1488
self .value_selected = self .labels [index ].get_text ()
1469
1489
self ._buttons .get_facecolor ()[:] = colors .to_rgba ("none" )
1470
1490
self ._buttons .get_facecolor ()[index ] = colors .to_rgba (self .activecolor )
1491
+ if self ._background is not None :
1492
+ self .canvas .restore_region (self ._background )
1471
1493
if hasattr (self , "_circles" ): # Remove once circles is removed.
1472
1494
for i , p in enumerate (self ._circles ):
1473
1495
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
+
1474
1502
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
+
1476
1508
if self .eventson :
1477
1509
self ._observers .process ('clicked' , self .labels [index ].get_text ())
1478
1510
@@ -1496,7 +1528,8 @@ def circles(self):
1496
1528
circles = self ._circles = [
1497
1529
Circle (xy = self ._buttons .get_offsets ()[i ], edgecolor = "black" ,
1498
1530
facecolor = self ._buttons .get_facecolor ()[i ],
1499
- radius = radius , transform = self .ax .transAxes )
1531
+ radius = radius , transform = self .ax .transAxes ,
1532
+ animated = self ._useblit )
1500
1533
for i in range (len (self .labels ))]
1501
1534
self ._buttons .set_visible (False )
1502
1535
for circle in circles :
0 commit comments