@@ -503,31 +503,38 @@ class CheckButtons(AxesWidget):
503
503
504
504
Connect to the CheckButtons with the :meth:`on_clicked` method
505
505
"""
506
- def __init__ (self , ax , labels , actives ):
506
+ def __init__ (self , ax , labels , actives = None ):
507
507
"""
508
508
Add check buttons to :class:`matplotlib.axes.Axes` instance *ax*
509
509
510
- *labels*
511
- A len(buttons) list of labels as strings
510
+ Parameters
511
+ ----------
512
+ ax : `~matplotlib.axes.Axes`
513
+ The parent axes for the widget.
514
+
515
+ labels : List[str]
516
+ The labels of the check buttons.
512
517
513
- * actives*
514
- A len(buttons) list of booleans indicating whether
515
- the button is active
518
+ actives : List[bool], optional
519
+ The initial check states of the buttons. The list must have the
520
+ same length as *labels*. If not given, all buttons are unchecked.
516
521
"""
517
522
AxesWidget .__init__ (self , ax )
518
523
519
524
ax .set_xticks ([])
520
525
ax .set_yticks ([])
521
526
ax .set_navigate (False )
522
527
528
+ if actives is None :
529
+ actives = [False ] * len (labels )
530
+
523
531
if len (labels ) > 1 :
524
532
dy = 1. / (len (labels ) + 1 )
525
533
ys = np .linspace (1 - dy , dy , len (labels ))
526
534
else :
527
535
dy = 0.25
528
536
ys = [0.5 ]
529
537
530
- cnt = 0
531
538
axcolor = ax .get_facecolor ()
532
539
533
540
self .labels = []
@@ -536,29 +543,28 @@ def __init__(self, ax, labels, actives):
536
543
537
544
lineparams = {'color' : 'k' , 'linewidth' : 1.25 ,
538
545
'transform' : ax .transAxes , 'solid_capstyle' : 'butt' }
539
- for y , label in zip (ys , labels ):
546
+ for y , label , active in zip (ys , labels , actives ):
540
547
t = ax .text (0.25 , y , label , transform = ax .transAxes ,
541
548
horizontalalignment = 'left' ,
542
549
verticalalignment = 'center' )
543
550
544
- w , h = dy / 2. , dy / 2.
545
- x , y = 0.05 , y - h / 2.
551
+ w , h = dy / 2 , dy / 2
552
+ x , y = 0.05 , y - h / 2
546
553
547
554
p = Rectangle (xy = (x , y ), width = w , height = h , edgecolor = 'black' ,
548
555
facecolor = axcolor , transform = ax .transAxes )
549
556
550
557
l1 = Line2D ([x , x + w ], [y + h , y ], ** lineparams )
551
558
l2 = Line2D ([x , x + w ], [y , y + h ], ** lineparams )
552
559
553
- l1 .set_visible (actives [ cnt ] )
554
- l2 .set_visible (actives [ cnt ] )
560
+ l1 .set_visible (active )
561
+ l2 .set_visible (active )
555
562
self .labels .append (t )
556
563
self .rectangles .append (p )
557
564
self .lines .append ((l1 , l2 ))
558
565
ax .add_patch (p )
559
566
ax .add_line (l1 )
560
567
ax .add_line (l2 )
561
- cnt += 1
562
568
563
569
self .connect_event ('button_press_event' , self ._clicked )
564
570
0 commit comments