IPython Notebook Interactive Feature: How to Set Slider Range

I wrote code in an Ipython notebook to create a sigmoid function, controlled by the parameters a, which determines the position of the sigmoid center, and b, which determines 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(-(xa)/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()) 

I wanted to add interactivity for parameters a and b, so I re-wrote the function as shown 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(-(xa)/(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 a way to limit the range of sliders to symmetrical (e.g. near zero)? It seems to me that this is impossible by simply setting the initial value for the parameters.

+7
python interactive slider widget ipython-notebook
source share
1 answer

You can create widgets manually and bind them to variables in the interactive function. Thus, you are much more flexible and can adapt these widgets to your needs.

In this example, two different sliders are created and their maximum, minimum, step and initial values ​​are set and used 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) 
+5
source share

All Articles