您好,登錄后才能下訂單哦!
怎么在Python中使用Slider組件調整曲線參數?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
一 代碼
import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider,Button,RadioButtons fig, ax = plt.subplots() plt.subplots_adjust(left=0.1, bottom=0.25) t = np.arange(0.0,1.0,0.001) #初始振幅與頻率,并繪制初始圖形 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組件,分別設置位置/尺寸、背景色和初始值 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組件的當前值,并以此來更新圖形 amp = samp.val freq = sfreq.val 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(ampValue) 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) #創建按鈕組件,用來恢復初始值 resetax = plt.axes([0.8,0.025,0.1,0.04]) button =Button(resetax,'Reset', color=axColor, hovercolor='yellow') def reset(event): sfreq.reset() samp.reset() button.on_clicked(reset) ###用來控制圖形顏色的 ##rax = plt.axes([0.025, 0.5, 0.15, 0.15], axisbg=axColor) ##radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0) ##def colorfunc(label): ## l.set_color(label) ## fig.canvas.draw_idle() ##radio.on_clicked(colorfunc) plt.show()
二 運行結果
看完上述內容,你們掌握怎么在Python中使用Slider組件調整曲線參數的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。