我试图了解如何使用Bokeh和CustomJS以及Python函数在RadioButtonGroup中使用交互性。我已经调整the example provided at the Bokeh site来绘制y = x ^ f。我没有使用滑块作为功率f,而是想在两个幂之间切换,f = 0.5和f = 0.2。我已经按照手册并使用Jupyter笔记本在我的代码中插入了RadioButtonGroup。按钮显示和响应,但我无法通过切换按钮获得任何回调响应。
任何帮助将不胜感激。
from math import pi
from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Slider, TextInput, RadioButtonGroup
from bokeh.plotting import Figure, output_notebook
output_notebook()
x = [x*0.005 for x in range(0, 200)]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = Figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
def callback(source=source, input1=input1, window=None):
data = source.data
m= input1.active
if m==0:
f=.5
else:
f=2
x, y = data['x'], data['y']
for i in range(len(x)):
y[i] = window.Math.pow(x[i], f)
source.trigger('change')
input1 = RadioButtonGroup(labels=["power = .5", "power = 2."], active=0)
input1.button_type="success"
input1.js_on_change('active', CustomJS.from_py_func(callback))
layout = column(input1, plot)
show(layout)

答案 0 :(得分:0)
我无法让bokeh.models.RadioButtonGroup
生成回调,但bokeh.models.RadioGroup
我可以(我使用散景版0.12.4)也许新版本可以促使RadioButtonGroup
生成打回来)。此外,您在声明之前引用input1
。它不需要作为回调函数的输入,你可以使用cb_obj
。参见:
import bokeh
import bokeh.plotting
bokeh.io.output_notebook()
x = [x*0.005 for x in range(0, 200)]
y = x
source = bokeh.models.ColumnDataSource(data=dict(x=x, y=y))
plot = bokeh.plotting.figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
def callback(source=source, window=None):
data = source.data
m = cb_obj.active
if m==0:
f=.5
else:
f=2
x, y = data['x'], data['y']
for i in range(len(x)):
y[i] = window.Math.pow(x[i], f)
source.trigger('change')
input1 = bokeh.models.RadioGroup(labels=["power = .5", "power = 2."],active=0,
callback=bokeh.models.CustomJS.from_py_func(callback))
layout = bokeh.layouts.column(input1, plot)
bokeh.io.show(layout)