|
| 1 | +from pylab import * |
| 2 | +from matplotlib.widgets import Slider, Button, RadioButtons |
| 3 | + |
| 4 | +ax = subplot(111) |
| 5 | +subplots_adjust(left=0.25, bottom=0.25) |
| 6 | +t = arange(0.0, 1.0, 0.001) |
| 7 | +a0 = 5 |
| 8 | +f0 = 3 |
| 9 | +s = a0*sin(2*pi*f0*t) |
| 10 | +l, = plot(t,s, lw=2, color='red') |
| 11 | +axis([0, 1, -10, 10]) |
| 12 | + |
| 13 | +axcolor = 'lightgoldenrodyellow' |
| 14 | +axfreq = axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor) |
| 15 | +axamp = axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor) |
| 16 | + |
| 17 | +sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0) |
| 18 | +samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0) |
| 19 | + |
| 20 | +def update(val): |
| 21 | + amp = samp.val |
| 22 | + freq = sfreq.val |
| 23 | + l.set_ydata(amp*sin(2*pi*freq*t)) |
| 24 | + draw() |
| 25 | +sfreq.on_changed(update) |
| 26 | +samp.on_changed(update) |
| 27 | + |
| 28 | +resetax = axes([0.8, 0.025, 0.1, 0.04]) |
| 29 | +button = Button(resetax, 'Reset', color=axcolor, hovercolor=0.975) |
| 30 | +def reset(event): |
| 31 | + sfreq.reset() |
| 32 | + samp.reset() |
| 33 | +button.on_clicked(reset) |
| 34 | + |
| 35 | +rax = axes([0.025, 0.5, 0.15, 0.15], axisbg=axcolor) |
| 36 | +radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0) |
| 37 | +def colorfunc(label): |
| 38 | + l.set_color(label) |
| 39 | + draw() |
| 40 | +radio.on_clicked(colorfunc) |
| 41 | + |
| 42 | +show() |
| 43 | + |
0 commit comments