Skip to content

Commit 8c39032

Browse files
authored
Merge pull request #10942 from timhoffm/widgets
Make active param in CheckBottons optional, default false
2 parents 3664c83 + b9e15d2 commit 8c39032

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

lib/matplotlib/widgets.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -503,31 +503,38 @@ class CheckButtons(AxesWidget):
503503
504504
Connect to the CheckButtons with the :meth:`on_clicked` method
505505
"""
506-
def __init__(self, ax, labels, actives):
506+
def __init__(self, ax, labels, actives=None):
507507
"""
508508
Add check buttons to :class:`matplotlib.axes.Axes` instance *ax*
509509
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.
512517
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.
516521
"""
517522
AxesWidget.__init__(self, ax)
518523

519524
ax.set_xticks([])
520525
ax.set_yticks([])
521526
ax.set_navigate(False)
522527

528+
if actives is None:
529+
actives = [False] * len(labels)
530+
523531
if len(labels) > 1:
524532
dy = 1. / (len(labels) + 1)
525533
ys = np.linspace(1 - dy, dy, len(labels))
526534
else:
527535
dy = 0.25
528536
ys = [0.5]
529537

530-
cnt = 0
531538
axcolor = ax.get_facecolor()
532539

533540
self.labels = []
@@ -536,29 +543,28 @@ def __init__(self, ax, labels, actives):
536543

537544
lineparams = {'color': 'k', 'linewidth': 1.25,
538545
'transform': ax.transAxes, 'solid_capstyle': 'butt'}
539-
for y, label in zip(ys, labels):
546+
for y, label, active in zip(ys, labels, actives):
540547
t = ax.text(0.25, y, label, transform=ax.transAxes,
541548
horizontalalignment='left',
542549
verticalalignment='center')
543550

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
546553

547554
p = Rectangle(xy=(x, y), width=w, height=h, edgecolor='black',
548555
facecolor=axcolor, transform=ax.transAxes)
549556

550557
l1 = Line2D([x, x + w], [y + h, y], **lineparams)
551558
l2 = Line2D([x, x + w], [y, y + h], **lineparams)
552559

553-
l1.set_visible(actives[cnt])
554-
l2.set_visible(actives[cnt])
560+
l1.set_visible(active)
561+
l2.set_visible(active)
555562
self.labels.append(t)
556563
self.rectangles.append(p)
557564
self.lines.append((l1, l2))
558565
ax.add_patch(p)
559566
ax.add_line(l1)
560567
ax.add_line(l2)
561-
cnt += 1
562568

563569
self.connect_event('button_press_event', self._clicked)
564570

0 commit comments

Comments
 (0)