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