网站建设资讯

NEWS

网站建设资讯

怎么在Python中使用Slider组件调整曲线参数-创新互联

怎么在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组件调整曲线参数

看完上述内容,你们掌握怎么在Python中使用Slider组件调整曲线参数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联成都网站设计公司行业资讯频道,感谢各位的阅读!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:怎么在Python中使用Slider组件调整曲线参数-创新互联
文章路径:http://cdweb.net/article/dgggpj.html