-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhradiobutton.py
61 lines (54 loc) · 2.13 KB
/
hradiobutton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from matplotlib.widgets import AxesWidget, RadioButtons
from matplotlib import cbook
class HRadioButtons(RadioButtons):
def __init__(self, ax, labels, active=0, activecolor='blue', size=49,
orientation="horizontal", **kwargs):
"""
Add radio buttons to an `~.axes.Axes`.
Parameters
----------
ax : `~matplotlib.axes.Axes`
The axes to add the buttons to.
labels : list of str
The button labels.
active : int
The index of the initially selected button.
activecolor : color
The color of the selected button.
size : float
Size of the radio buttons
orientation : str
The orientation of the buttons: 'vertical' (default), or 'horizontal'.
Further parameters are passed on to `Legend`.
"""
AxesWidget.__init__(self, ax)
self.activecolor = activecolor
axcolor = ax.get_facecolor()
self.value_selected = None
ax.set_xticks([])
ax.set_yticks([])
ax.set_navigate(False)
circles = []
for i, label in enumerate(labels):
if i == active:
self.value_selected = label
facecolor = activecolor
else:
facecolor = axcolor
p = ax.scatter([], [], s=size, marker="o", edgecolor='black', facecolor=facecolor)
circles.append(p)
if orientation == "horizontal":
kwargs.update(ncol=len(labels), mode="expand")
kwargs.setdefault("frameon", False)
self.box = ax.legend(circles, labels, loc="center", **kwargs)
self.labels = self.box.texts
self.circles = self.box.legendHandles
for c in self.circles:
c.set_picker(5)
self._observers = cbook.CallbackRegistry()
self.connect_event('pick_event', self._clicked)
def _clicked(self, event):
if self.ignore(event) or event.mouseevent.button != 1 or event.mouseevent.inaxes != self.ax:
return
if event.artist in self.circles:
self.set_active(self.circles.index(event.artist))