a code Import NumPy as NP import Matplotlib.pyplot as PLT from matplotlib.widgets importslider,button,radiobuttons fig, ax = PLT . Subplots () Plt.subplots_adjust (left=0.1, bottom=0.25) t = Np.arange (0.0,1.0,0.001) #初始振幅与频率 and draw the initial graphics a0, f0=5,3 s = a0*np . Sin (2*np.pi*f0*t) l,= Plt.plot (t, S, lw=2, color= ' red ') #设置坐标轴刻度范围 plt.axis ([0,1,-10,10]) Axcolor = ' Lightgoldenrodyellow ' #创建两个Slider组件, set position/size, background color and initial value axfreq = Plt.axes ([0.1,0.1,0.75,0.03], axisbg=axcolor) Sfreq = Slider (axfreq, ' freq ', 0.1,30.0, valinit=f0) Axamp = Plt.axes ([0.1,0.15,0.75,0.03], Axisbg=axcolor) Samp =Slider (Axamp, ' Amp ', 0.1,10.0, valinit=a0 #为Slider组件设置事件处理函数 def update (event): #获取Slider组件的当前值, and to update the graphics amp = samp.val Freq = Sfreq.va L L.set_ydata (Amp*np.sin (2*np.pi*freq*t)) Plt.draw () #fig. Canvas.draw_idle () sfreq.on_changed (update) Samp.on_ Changed (UPDATE) def adjustslidervalue (event): Ampvalue = Samp.val +0.05. If Ampvalue >10:ampvalue =0.1 samp.set_val (amp Value) Freqvalue = Sfreq.val +0.05 if Freqvalue >30:freqvalue =0.1 Sfreq.set_val (freqvalue) Update (event) Axadjust = Plt.axes ([0.6,0.025,0.1,0.04]) Buttonadjust =button (AxAdjust, ' Adjust ', Color=axcolor, hovercolor= ' Red ') buttonadjust.on_clicked (adjustslidervalue) #创建按钮组件, used to restore the initial value Resetax = Plt.axes ([0.8,0.025,0.1,0.04]) button =button (resetax, ' Reset ', Color=axcolor, hovercolor= ' yellow ') def Reset (event): Sfreq.reset ()