diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index f9e8321d8bff..f8d1c47ce0e6 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -503,16 +503,21 @@ class CheckButtons(AxesWidget): Connect to the CheckButtons with the :meth:`on_clicked` method """ - def __init__(self, ax, labels, actives): + def __init__(self, ax, labels, actives=None): """ Add check buttons to :class:`matplotlib.axes.Axes` instance *ax* - *labels* - A len(buttons) list of labels as strings + Parameters + ---------- + ax : `~matplotlib.axes.Axes` + The parent axes for the widget. + + labels : List[str] + The labels of the check buttons. - *actives* - A len(buttons) list of booleans indicating whether - the button is active + actives : List[bool], optional + The initial check states of the buttons. The list must have the + same length as *labels*. If not given, all buttons are unchecked. """ AxesWidget.__init__(self, ax) @@ -520,6 +525,9 @@ def __init__(self, ax, labels, actives): ax.set_yticks([]) ax.set_navigate(False) + if actives is None: + actives = [False] * len(labels) + if len(labels) > 1: dy = 1. / (len(labels) + 1) ys = np.linspace(1 - dy, dy, len(labels)) @@ -527,7 +535,6 @@ def __init__(self, ax, labels, actives): dy = 0.25 ys = [0.5] - cnt = 0 axcolor = ax.get_facecolor() self.labels = [] @@ -536,13 +543,13 @@ def __init__(self, ax, labels, actives): lineparams = {'color': 'k', 'linewidth': 1.25, 'transform': ax.transAxes, 'solid_capstyle': 'butt'} - for y, label in zip(ys, labels): + for y, label, active in zip(ys, labels, actives): t = ax.text(0.25, y, label, transform=ax.transAxes, horizontalalignment='left', verticalalignment='center') - w, h = dy / 2., dy / 2. - x, y = 0.05, y - h / 2. + w, h = dy / 2, dy / 2 + x, y = 0.05, y - h / 2 p = Rectangle(xy=(x, y), width=w, height=h, edgecolor='black', facecolor=axcolor, transform=ax.transAxes) @@ -550,15 +557,14 @@ def __init__(self, ax, labels, actives): l1 = Line2D([x, x + w], [y + h, y], **lineparams) l2 = Line2D([x, x + w], [y, y + h], **lineparams) - l1.set_visible(actives[cnt]) - l2.set_visible(actives[cnt]) + l1.set_visible(active) + l2.set_visible(active) self.labels.append(t) self.rectangles.append(p) self.lines.append((l1, l2)) ax.add_patch(p) ax.add_line(l1) ax.add_line(l2) - cnt += 1 self.connect_event('button_press_event', self._clicked)