Adjusting curve parameters using the slider component

Source: Internet
Author: User
Tags sin

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.