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