在 matplotlib 小部件(Python 2.7)中单击按钮时提取/打印滑块值 [英] Extracting / printing slider value when button is clicked in matplotlib widget (Python 2.7)

查看:63
本文介绍了在 matplotlib 小部件(Python 2.7)中单击按钮时提取/打印滑块值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Matplotlib 滑块小部件的模板.我正在使用 this 模板.如何更改重置"按钮下的脚本以打印出 Amp 和 Freq 滑块的值?

I am using the template for the Matplotlib slider widget. I am using this template. How can I alter the script under the "Reset" button to print out the value of the Amp and Freq sliders?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

ax = plt.subplot(111)
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 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'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axamp  = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)

def update(val):
    amp = samp.val
    freq = sfreq.val
    l.set_ydata(amp*np.sin(2*np.pi*freq*t))
    plt.draw()
sfreq.on_changed(update)
samp.on_changed(update)

resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')

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)
    plt.draw()
radio.on_clicked(colorfunc)

plt.show()

推荐答案

在上面的代码中,将 reset 函数替换为类似

In the code above replace the reset function by something like

def reset(event):
    print "Amplitude: ",  samp.val
    #sfreq.reset()
    #samp.reset()

这篇关于在 matplotlib 小部件(Python 2.7)中单击按钮时提取/打印滑块值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆