IPython notebook interactive function: how to set the slider range(IPython notebook交互功能:如何设置滑块范围)
问题描述
我在 Ipython notebook 中编写了以下代码来生成一个 sigmoid 函数,该函数由参数 a 控制,该参数定义 sigmoid 中心的位置,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交互功能:如何设置滑块范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:IPython notebook交互功能:如何设置滑块范围
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
