- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwidgets.py
148 lines (125 loc) · 3.9 KB
/
widgets.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
======
Slider
======
In this example, sliders are used to control the frequency and amplitude of
a sine wave.
"""
importinspect
importnumpyasnp
importmatplotlib.pyplotasplt
frommatplotlib.widgetsimportSlider, Button
fromdata_prototype.artistimportCompatibilityArtistasCA
fromdata_prototype.lineimportLine
fromdata_prototype.containersimportFuncContainer
fromdata_prototype.descriptionimportDesc
fromdata_prototype.conversion_edgeimportFuncEdge
classSliderContainer(FuncContainer):
def__init__(self, xfuncs, /, **sliders):
self._sliders=sliders
forsliderinsliders.values():
slider.on_changed(
lambda_, sld=slider: sld.ax.figure.canvas.draw_idle(),
)
defget_needed_keys(f, offset=1):
returntuple(inspect.signature(f).parameters)[offset:]
super().__init__(
{
k: (
s,
# this line binds the correct sliders to the functions
# and makes lambdas that match the API FuncContainer needs
lambdax, keys=get_needed_keys(f), f=f: f(
x, *(sliders[k].valforkinkeys)
),
)
fork, (s, f) inxfuncs.items()
},
)
def_query_hash(self, graph, parent_coordinates):
key=super()._query_hash(graph, parent_coordinates)
# inject the slider values into the hashing logic
returnhash((key, tuple(s.valforsinself._sliders.values())))
# Define initial parameters
init_amplitude=5
init_frequency=3
# Create the figure and the line that we will manipulate
fig, ax=plt.subplots()
ax.set_xlim(0, 1)
ax.set_ylim(-7, 7)
ax.set_xlabel("Time [s]")
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.25, bottom=0.25, right=0.75)
# Make a horizontal slider to control the frequency.
axfreq=fig.add_axes([0.25, 0.1, 0.65, 0.03])
freq_slider=Slider(
ax=axfreq,
label="Frequency [Hz]",
valmin=0.1,
valmax=30,
valinit=init_frequency,
)
# Make a vertically oriented slider to control the amplitude
axamp=fig.add_axes([0.1, 0.25, 0.0225, 0.63])
amp_slider=Slider(
ax=axamp,
label="Amplitude",
valmin=0,
valmax=10,
valinit=init_amplitude,
orientation="vertical",
)
# Make a vertically oriented slider to control the phase
axphase=fig.add_axes([0.85, 0.25, 0.0225, 0.63])
phase_slider=Slider(
ax=axphase,
label="Phase [rad]",
valmin=-2*np.pi,
valmax=2*np.pi,
valinit=0,
orientation="vertical",
)
# pick a cyclic color map
cmap=plt.get_cmap("twilight")
# set up the data container
fc=SliderContainer(
{
# the x data does not need the sliders values
"x": (("N",), lambdat: t),
"y": (
("N",),
# the y data needs all three sliders
lambdat, amplitude, frequency, phase: amplitude
*np.sin(2*np.pi*frequency*t+phase),
),
# the color data has to take the x (because reasons), but just
# needs the phase
"color": ((1,), lambda_, phase: phase),
},
# bind the sliders to the data container
amplitude=amp_slider,
frequency=freq_slider,
phase=phase_slider,
)
lw=Line(
fc,
# color map phase (scaled to 2pi and wrapped to [0, 1])
[
FuncEdge.from_func(
"color",
lambdacolor: cmap((color/ (2*np.pi)) %1),
{"color": Desc((1,))},
{"color": Desc((), "display")},
)
],
linewidth=5.0,
linestyle="-",
)
ax.add_artist(CA(lw))
# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
resetax=fig.add_axes([0.8, 0.025, 0.1, 0.04])
button=Button(resetax, "Reset", hovercolor="0.975")
button.on_clicked(
lambda_: [sld.reset() forsldin (freq_slider, amp_slider, phase_slider)]
)
plt.show()