Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用带Pythonic回调的Bokeh滑块在图像中滑动_Javascript_Python_Slider_Bokeh - Fatal编程技术网

Javascript 使用带Pythonic回调的Bokeh滑块在图像中滑动

Javascript 使用带Pythonic回调的Bokeh滑块在图像中滑动,javascript,python,slider,bokeh,Javascript,Python,Slider,Bokeh,受上一个示例的启发,我尝试实现一个滑块,以在大量收集的数据中滑动(基本上是生物数据的延时)。我没有在滑块上使用自定义javascript回调,而是尝试使用小部件。我不知道这是否可行。找到我的最小工作示例。它正确地显示了滑块和图像,但似乎没有进行更新 ##Creating the 15 different pictures #Want to make 15 different pictures of a certain field function evaluated on a grid of i

受上一个示例的启发,我尝试实现一个滑块,以在大量收集的数据中滑动(基本上是生物数据的延时)。我没有在滑块上使用自定义javascript回调,而是尝试使用小部件。我不知道这是否可行。找到我的最小工作示例。它正确地显示了滑块和图像,但似乎没有进行更新

##Creating the 15 different pictures
#Want to make 15 different pictures of a certain field function evaluated on a grid of img_size_x x img_size_y
import numpy as np
img_size_x,img_size_y = (512,512)
variations=15

#Make the field
xx,yy=np.meshgrid( np.arange(img_size_x),np.arange(img_size_y))

#Broadcast the field into as many copies as there are variations to make use of the ufuncs
xx= np.tile(xx,variations).reshape(variations,img_size_x,img_size_y)
yy= np.asarray(map(np.transpose,np.tile(yy.T,variations).reshape(variations,img_size_x,img_size_y)))

varied_parameter=np.linspace(.01,0.5,variations) #frequencies of a sin/cos function, for example
varied_parameter=np.repeat(varied_parameter,img_size_x*img_size_y).reshape(variations,img_size_x,img_size_y) #broadcast

matrix_images=np.cos(varied_parameter*xx)+np.sin(varied_parameter*yy) # field function evaluated for diff frequencies.

##Creation of the Bokeh interface to slide through these pictures
from bokeh.plotting import figure, show, output_file, output_notebook
from bokeh.models import ColumnDataSource
from bokeh.layouts import row, widgetbox
from bokeh.models.widgets import Slider
import bokeh.palettes as pal
output_notebook()

data=matrix_images[0] #starting value for the column data source
source = ColumnDataSource(dict(image=[data])) #the figure.image function takes a vector of matrices
image_sl = Slider(title="Image number", value=0, start=0, end=variations-1, step=1) #slider to go through the images
def update_img(attrname, old, new):
    curr_value = image_sl.value
    x=matrix_images[int(curr_value)] #make sure index is int to select image number 'curr_value'
    source.data = dict(image=[x])

image_sl.on_change('value', update_img) #give slider its callback function
inputs = widgetbox(image_sl) #wrap the slider into a display object


p = figure(x_range=(0, 10), y_range=(0, 10))
# must give a vector of image data for image parameter
p.image('image', source=source,x=0, y=0, dw=10, dh=10, palette=pal.Greys256)

show(row([p,image_sl]) ) # open a browser

您尝试使用的更新类型仅适用于Bokeh服务器。当
output\u notebook
output\u file
show
一起使用时,生成的输出是带有嵌入JavaScript的HTML,用于呈现实际绘图。这意味着这些绘图应被视为在浏览器中运行的独立文件。这意味着这些绘图不能直接访问或运行任何Python代码

Bokeh提供了几种用Python编写绘图回调的方法。第一种是使用Bokeh服务器。你可以读到它。由于您编写了回调来使用Bokeh服务器,所以以这种方式工作非常容易。我注释掉了这些行,这导致了一个错误

xx= np.tile(xx,variations).reshape(...
yy= np.asarray(map(np.transpose, ...
然后从bokeh.io import curdoc添加curdoc
的导入,并将
show(行([p,图像])
替换为
curdoc()。添加根(行([p,图像])
)。从这里开始,可以使用
$bokeh serve--show name_of_your_file.py
运行该示例

如果你想要在笔记本或独立文件中运行的东西。您还可以选择使用PyScript回调。这个回调看起来像python,它与python代码写在同一个文件中。但是,它将被解释为一种称为PyScript的语言,用于将“Python代码”编译为JavaAcript。同样,这将无法访问python运行时环境。可以将Bokeh对象传递给这些回调,但仅此而已。这可能不适合您的用例,因为您的回调需要访问
matrix\u图像
。你可以读更多。我建议您阅读关于添加交互的整个章节,其中有很多关于如何使用CustomJS回调的好例子


另一个选择是使用Jupyter笔记本小部件。不过,此解决方案只能在笔记本中使用。您可以阅读此方法。

如果有一种方法可以编写纯Python回调来完成类似与Bokeh交互的正弦曲线图之类的事情,那就太好了。这似乎是一个相当基本的功能(至少与制作套索和显示多个带有链接数据的绘图一样基本,我可以使用Python+Bokeh来完成)。坦白地说,我有很多事情要做,现在向我的堆中添加javascript是不可能的。现在我将继续使用
ipywidgets
——虽然没有那么好,但它是Python。