@@ -1456,7 +1456,8 @@ class RadioButtons(AxesWidget):
1456
1456
The label text of the currently selected button.
1457
1457
"""
1458
1458
1459
- def __init__ (self , ax , labels , active = 0 , activecolor = 'blue' ):
1459
+ def __init__ (self , ax , labels , active = 0 , activecolor = 'blue' , * ,
1460
+ useblit = True ):
1460
1461
"""
1461
1462
Add radio buttons to an `~.axes.Axes`.
1462
1463
@@ -1470,6 +1471,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1470
1471
The index of the initially selected button.
1471
1472
activecolor : color
1472
1473
The color of the selected button.
1474
+ useblit : bool, default: True
1475
+ Use blitting for faster drawing if supported by the backend.
1476
+ See the tutorial :doc:`/tutorials/advanced/blitting` for details.
1473
1477
"""
1474
1478
super ().__init__ (ax )
1475
1479
self .activecolor = activecolor
@@ -1482,19 +1486,34 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
1482
1486
ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
1483
1487
text_size = mpl .rcParams ["font.size" ] / 2
1484
1488
1489
+ self ._useblit = useblit and self .canvas .supports_blit
1490
+ self ._background = None
1491
+
1485
1492
self .labels = [
1486
1493
ax .text (0.25 , y , label , transform = ax .transAxes ,
1487
1494
horizontalalignment = "left" , verticalalignment = "center" )
1488
1495
for y , label in zip (ys , labels )]
1489
1496
self ._buttons = ax .scatter (
1490
1497
[.15 ] * len (ys ), ys , transform = ax .transAxes , s = text_size ** 2 ,
1491
1498
c = [activecolor if i == active else "none" for i in range (len (ys ))],
1492
- edgecolor = "black" )
1499
+ edgecolor = "black" , animated = self . _useblit )
1493
1500
1494
1501
self .connect_event ('button_press_event' , self ._clicked )
1502
+ if self ._useblit :
1503
+ self .connect_event ('draw_event' , self ._clear )
1495
1504
1496
1505
self ._observers = cbook .CallbackRegistry (signals = ["clicked" ])
1497
1506
1507
+ def _clear (self , event ):
1508
+ """Internal event handler to clear the buttons."""
1509
+ if self .ignore (event ):
1510
+ return
1511
+ self ._background = self .canvas .copy_from_bbox (self .ax .bbox )
1512
+ self .ax .draw_artist (self ._buttons )
1513
+ if hasattr (self , "_circles" ):
1514
+ for circle in self ._circles :
1515
+ self .ax .draw_artist (circle )
1516
+
1498
1517
def _clicked (self , event ):
1499
1518
if self .ignore (event ) or event .button != 1 or event .inaxes != self .ax :
1500
1519
return
@@ -1533,8 +1552,20 @@ def set_active(self, index):
1533
1552
if hasattr (self , "_circles" ): # Remove once circles is removed.
1534
1553
for i , p in enumerate (self ._circles ):
1535
1554
p .set_facecolor (self .activecolor if i == index else "none" )
1555
+ if self .drawon and self ._useblit :
1556
+ self .ax .draw_artist (p )
1536
1557
if self .drawon :
1537
- self .ax .figure .canvas .draw ()
1558
+ if self ._useblit :
1559
+ if self ._background is not None :
1560
+ self .canvas .restore_region (self ._background )
1561
+ self .ax .draw_artist (self ._buttons )
1562
+ if hasattr (self , "_circles" ):
1563
+ for p in self ._circles :
1564
+ self .ax .draw_artist (p )
1565
+ self .canvas .blit (self .ax .bbox )
1566
+ else :
1567
+ self .canvas .draw ()
1568
+
1538
1569
if self .eventson :
1539
1570
self ._observers .process ('clicked' , self .labels [index ].get_text ())
1540
1571
@@ -1558,7 +1589,8 @@ def circles(self):
1558
1589
circles = self ._circles = [
1559
1590
Circle (xy = self ._buttons .get_offsets ()[i ], edgecolor = "black" ,
1560
1591
facecolor = self ._buttons .get_facecolor ()[i ],
1561
- radius = radius , transform = self .ax .transAxes )
1592
+ radius = radius , transform = self .ax .transAxes ,
1593
+ animated = self ._useblit )
1562
1594
for i in range (len (self .labels ))]
1563
1595
self ._buttons .set_visible (False )
1564
1596
for circle in circles :
0 commit comments