Note
Go to the endto download the full example code.
Buttons#
Constructing a simple button GUI to modify a sine wave.
Thenext andprevious button widget helps visualize the wave withnew frequencies.
importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.widgetsimportButtonfreqs=np.arange(2,20,3)fig,ax=plt.subplots()fig.subplots_adjust(bottom=0.2)t=np.arange(0.0,1.0,0.001)s=np.sin(2*np.pi*freqs[0]*t)l,=ax.plot(t,s,lw=2)classIndex:ind=0defnext(self,event):self.ind+=1i=self.ind%len(freqs)ydata=np.sin(2*np.pi*freqs[i]*t)l.set_ydata(ydata)plt.draw()defprev(self,event):self.ind-=1i=self.ind%len(freqs)ydata=np.sin(2*np.pi*freqs[i]*t)l.set_ydata(ydata)plt.draw()callback=Index()axprev=fig.add_axes((0.7,0.05,0.1,0.075))axnext=fig.add_axes((0.81,0.05,0.1,0.075))bnext=Button(axnext,'Next')bnext.on_clicked(callback.next)bprev=Button(axprev,'Previous')bprev.on_clicked(callback.prev)plt.show()

References
The use of the following functions, methods, classes and modules is shownin this example: