如何设置滑块范围

如何设置滑块范围

本文介绍了IPython Notebook交互式功能:如何设置滑块范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ipython笔记本中编写了以下代码,以生成由参数a控制的sigmoid函数,其中a定义了S型中心的位置,b定义了其宽度:

I wrote the code below in Ipython notebook to generate a sigmoid function controlled by parameters a which defines the position of the sigmoid center, and b which defines its width:

%matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt

def sigmoid(x,a,b):
    #sigmoid function with parameters a = center; b = width
    s= 1/(1+np.exp(-(x-a)/b))
    return 100.0*(s-min(s))/(max(s)-min(s)) # normalize sigmoid to 0-100

x = np.linspace(0,10,256)
sigm = sigmoid(x, a=5, b=1)
fig = plt.figure(figsize=(24,6))
ax1 = fig.add_subplot(2, 1, 1)
ax1.set_xticks([])
ax1.set_xticks([])
plt.plot(x,sigm,lw=2,color='black')
plt.xlim(x.min(), x.max())

我想为参数a和b添加交互性,因此我重新编写了如下函数:

I wanted to add interactivity for parameters a and b so I re-wrote the function as below:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython.html.widgets import interactive
from IPython.display import display

def sigmoid_demo(a=5,b=1):
    x = np.linspace(0,10,256)
    s = 1/(1+np.exp(-(x-a)/(b+0.1))) # +0.1 to avoid dividing by 0
    sn = 100.0*(s-min(s))/(max(s)-min(s)) # normalize sigmoid to 0-100
    fig = plt.figure(figsize=(24,6))
    ax1 = fig.add_subplot(2, 1, 1)
    ax1.set_xticks([])
    ax1.set_yticks([])
    plt.plot(x,sn,lw=2,color='black')
    plt.xlim(x.min(), x.max())

w=widgets.interactive(sigmoid_demo,a=5,b=1)
display(w)

是否有任何方法可以将滑块的范围设置为对称(例如大约为零)?在我看来,仅通过设置参数的起始值是不可能的.

Is there any way to se the range of the sliders to be symmetrical (for example around zero)? It does not seem to me to be possible by just setting the starting value for the parameters.

推荐答案

您可以手动创建小部件,并将其绑定到interactive函数中的变量.这样,您将更加灵活,并可以根据需要定制这些小部件.

You can create widgets manually and bind them to variables in the interactive function. This way you are much more flexible and can tailor those widgets to your needs.

此示例创建两个不同的滑块,并设置它们的最大值,最小值,步长和初始值,并在interactive函数中使用它们.

This example creates two different sliders and sets their max, min, stepsize and initial value and uses them in the interactive function.

a_slider = widgets.IntSliderWidget(min=-5, max=5, step=1, value=0)
b_slider = widgets.FloatSliderWidget(min=-5, max=5, step=0.3, value=0)
w=widgets.interactive(sigmoid_demo,a=a_slider,b=b_slider)
display(w)

这篇关于IPython Notebook交互式功能:如何设置滑块范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:23