Skip to content

Make active param in CheckBottons optional, default false #10942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,31 +503,38 @@ 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)

ax.set_xticks([])
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))
else:
dy = 0.25
ys = [0.5]

cnt = 0
axcolor = ax.get_facecolor()

self.labels = []
Expand All @@ -536,29 +543,28 @@ 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)

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)

Expand Down