使用Slider元件調整曲線引數

2021-08-11 03:53:04 字數 4019 閱讀 4067

一 **

import

numpy

asnp

import

matplotlib

.pyplot

asplt

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

()二 執行結果

大小: 36.8 kb

微信小程式slider元件使用詳解

效果圖 wxml 設定step,當前設定步伐為5,當前值 是否在右側顯示當前值 設定最大值 最小值 滑動選擇器設定名稱 www.cppcns.comi fl 選擇 程式設計客棧ndchanging changeslider4 value wxss tui slider head,tui slider...

react 函式式元件使用 傳參

1 基於腳手架環境 2 function 首字母大寫 3 使用 首字母大寫 4 傳參 首字母大寫 鍵值對,變數需要 解析 5 函式式元件接收 function 首字母大寫 props 示例 index.js檔案 port react from react import reactdom from r...

vue父子元件傳參使用props

父子元件傳遞引數,使用props 總結下 驗證型別 支援的型別 string,number,boolean,array,symbol,object,function,date 基礎型別檢查 null 匹配任何型別 propa number,多個可能得型別 propb number,string 必填...