1"""
2=======
3Buttons
4=======
5
6Constructing a simple button GUI to modify a sine wave.
7
8The ``next`` and ``previous`` button widget helps visualize the wave with
9new frequencies.
10"""
11
12import numpy as np
13import matplotlib.pyplot as plt
14from matplotlib.widgets import Button
15
16freqs = np.arange(2, 20, 3)
17
18fig, ax = plt.subplots()
19plt.subplots_adjust(bottom=0.2)
20t = np.arange(0.0, 1.0, 0.001)
21s = np.sin(2*np.pi*freqs[0]*t)
22l, = plt.plot(t, s, lw=2)
23
24
25class Index(object):
26    ind = 0
27
28    def next(self, event):
29        self.ind += 1
30        i = self.ind % len(freqs)
31        ydata = np.sin(2*np.pi*freqs[i]*t)
32        l.set_ydata(ydata)
33        plt.draw()
34
35    def prev(self, event):
36        self.ind -= 1
37        i = self.ind % len(freqs)
38        ydata = np.sin(2*np.pi*freqs[i]*t)
39        l.set_ydata(ydata)
40        plt.draw()
41
42callback = Index()
43axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
44axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
45bnext = Button(axnext, 'Next')
46bnext.on_clicked(callback.next)
47bprev = Button(axprev, 'Previous')
48bprev.on_clicked(callback.prev)
49
50plt.show()
51