Skip to content

Commit d900676

Browse files
committed
*Buttons: share more __init__ functionality
1 parent 74842be commit d900676

1 file changed

Lines changed: 48 additions & 57 deletions

File tree

lib/matplotlib/widgets.py

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,37 @@ class _Buttons(AxesWidget):
10491049
public on the subclasses.
10501050
"""
10511051

1052+
def __init__(self, ax, labels, *, useblit=True, label_props=None, **kwargs):
1053+
super().__init__(ax)
1054+
1055+
ax.set_xticks([])
1056+
ax.set_yticks([])
1057+
ax.set_navigate(False)
1058+
1059+
self._useblit = useblit
1060+
1061+
self._buttons_ys = np.linspace(1, 0, len(labels)+2)[1:-1]
1062+
1063+
label_props = _expand_text_props(label_props)
1064+
1065+
self.labels = [
1066+
ax.text(0.25, y, label, transform=ax.transAxes,
1067+
horizontalalignment="left", verticalalignment="center",
1068+
**props)
1069+
for y, label, props in zip(self._buttons_ys, labels, label_props)]
1070+
text_size = np.array([text.get_fontsize() for text in self.labels]) / 2
1071+
1072+
self._init_props(text_size, **kwargs)
1073+
1074+
self.connect_event('button_press_event', self._clicked)
1075+
if self._useblit:
1076+
self.connect_event('draw_event', self._clear)
1077+
1078+
self._observers = cbook.CallbackRegistry(signals=["clicked"])
1079+
1080+
def _init_props(self, text_size, **kwargs):
1081+
raise NotImplementedError("This method should be defined in subclasses")
1082+
10521083
def _clear(self, event):
10531084
"""Internal event handler to clear the buttons."""
10541085
if self.ignore(event) or self.canvas.is_saving():
@@ -1171,40 +1202,24 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,
11711202
11721203
.. versionadded:: 3.7
11731204
"""
1174-
super().__init__(ax)
1175-
11761205
_api.check_isinstance((dict, None), label_props=label_props,
11771206
frame_props=frame_props, check_props=check_props)
11781207

1179-
ax.set_xticks([])
1180-
ax.set_yticks([])
1181-
ax.set_navigate(False)
1182-
1183-
if actives is None:
1184-
actives = [False] * len(labels)
1185-
1186-
self._useblit = useblit
1187-
1188-
self._buttons_ys = np.linspace(1, 0, len(labels)+2)[1:-1]
1189-
1190-
label_props = _expand_text_props(label_props)
1191-
self.labels = [
1192-
ax.text(0.25, y, label, transform=ax.transAxes,
1193-
horizontalalignment="left", verticalalignment="center",
1194-
**props)
1195-
for y, label, props in zip(self._buttons_ys, labels, label_props)]
1196-
text_size = np.array([text.get_fontsize() for text in self.labels]) / 2
1208+
super().__init__(ax, labels, useblit=useblit, label_props=label_props,
1209+
actives=actives, frame_props=frame_props,
1210+
check_props=check_props)
11971211

1212+
def _init_props(self, text_size, actives, frame_props, check_props):
11981213
frame_props = {
11991214
's': text_size**2,
12001215
'linewidth': 1,
12011216
**cbook.normalize_kwargs(frame_props, collections.PathCollection),
12021217
'marker': 's',
1203-
'transform': ax.transAxes,
1218+
'transform': self.ax.transAxes,
12041219
}
12051220
frame_props.setdefault('facecolor', frame_props.get('color', 'none'))
12061221
frame_props.setdefault('edgecolor', frame_props.pop('color', 'black'))
1207-
self._frames = ax.scatter(
1222+
self._frames = self.ax.scatter(
12081223
[0.15] * len(self._buttons_ys),
12091224
self._buttons_ys,
12101225
**frame_props,
@@ -1214,29 +1229,25 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,
12141229
's': text_size**2,
12151230
**cbook.normalize_kwargs(check_props, collections.PathCollection),
12161231
'marker': 'x',
1217-
'transform': ax.transAxes,
1232+
'transform': self.ax.transAxes,
12181233
'animated': self._useblit and self.canvas.supports_blit,
12191234
# TODO: This may need an update when switching out the canvas.
12201235
# Can set this to `_useblit` only and live with the animated=True
12211236
# overhead on unsupported backends.
12221237
}
12231238
check_props.setdefault('facecolor', check_props.pop('color', 'black'))
1224-
self._buttons = ax.scatter(
1239+
self._buttons = self.ax.scatter(
12251240
[0.15] * len(self._buttons_ys),
12261241
self._buttons_ys,
12271242
**check_props
12281243
)
1244+
if actives is None:
1245+
actives = [False] * len(self.labels)
12291246
# The user may have passed custom colours in check_props, so we need to
12301247
# create the checks (above), and modify the visibility after getting
12311248
# whatever the user set.
12321249
self._init_status(actives)
12331250

1234-
self.connect_event('button_press_event', self._clicked)
1235-
if self._useblit:
1236-
self.connect_event('draw_event', self._clear)
1237-
1238-
self._observers = cbook.CallbackRegistry(signals=["clicked"])
1239-
12401251
def set_frame_props(self, props):
12411252
"""
12421253
Set properties of the check button frames.
@@ -1698,8 +1709,6 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
16981709
16991710
.. versionadded:: 3.7
17001711
"""
1701-
super().__init__(ax)
1702-
17031712
_api.check_isinstance((dict, None), label_props=label_props,
17041713
radio_props=radio_props)
17051714

@@ -1713,33 +1722,21 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
17131722
'*activecolor* will be ignored.')
17141723
else:
17151724
activecolor = 'blue' # Default.
1725+
super().__init__(ax, labels, useblit=useblit, label_props=label_props,
1726+
active=active, activecolor=activecolor,
1727+
radio_props=radio_props)
17161728

17171729
self._activecolor = activecolor
17181730
self._initial_active = active
17191731
self.value_selected = labels[active]
17201732
self.index_selected = active
17211733

1722-
ax.set_xticks([])
1723-
ax.set_yticks([])
1724-
ax.set_navigate(False)
1725-
1726-
self._buttons_ys = np.linspace(1, 0, len(labels) + 2)[1:-1]
1727-
1728-
self._useblit = useblit
1729-
1730-
label_props = _expand_text_props(label_props)
1731-
self.labels = [
1732-
ax.text(0.25, y, label, transform=ax.transAxes,
1733-
horizontalalignment="left", verticalalignment="center",
1734-
**props)
1735-
for y, label, props in zip(self._buttons_ys, labels, label_props)]
1736-
text_size = np.array([text.get_fontsize() for text in self.labels]) / 2
1737-
1734+
def _init_props(self, text_size, active, activecolor, radio_props):
17381735
radio_props = {
17391736
's': text_size**2,
17401737
**radio_props,
17411738
'marker': 'o',
1742-
'transform': ax.transAxes,
1739+
'transform': self.ax.transAxes,
17431740
'animated': self._useblit and self.canvas.supports_blit,
17441741
# TODO: This may need an update when switching out the canvas.
17451742
# Can set this to `_useblit` only and live with the animated=True
@@ -1749,7 +1746,7 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
17491746
radio_props.setdefault('edgecolor', radio_props.get('color', 'black'))
17501747
radio_props.setdefault('facecolor',
17511748
radio_props.pop('color', activecolor))
1752-
self._buttons = ax.scatter(
1749+
self._buttons = self.ax.scatter(
17531750
[.15] * len(self._buttons_ys),
17541751
self._buttons_ys,
17551752
**radio_props,
@@ -1759,18 +1756,12 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
17591756
# the user set.
17601757
self._active_colors = self._buttons.get_facecolor()
17611758
if len(self._active_colors) == 1:
1762-
self._active_colors = np.repeat(self._active_colors, len(labels),
1759+
self._active_colors = np.repeat(self._active_colors, len(self.labels),
17631760
axis=0)
17641761
self._buttons.set_facecolor(
17651762
[activecolor if i == active else "none"
17661763
for i, activecolor in enumerate(self._active_colors)])
17671764

1768-
self.connect_event('button_press_event', self._clicked)
1769-
if self._useblit:
1770-
self.connect_event('draw_event', self._clear)
1771-
1772-
self._observers = cbook.CallbackRegistry(signals=["clicked"])
1773-
17741765
def set_radio_props(self, props):
17751766
"""
17761767
Set properties of the `.Text` labels.

0 commit comments

Comments
 (0)