[英]How to make a Bokeh scatter plot interactive (with slider)
I'm trying to make a scatter plot in Bokeh based on the simple example code posted here .我正在尝试根据此处发布的简单示例代码在 Bokeh 中制作散点图。
The following code produces a working demo for a line plot:以下代码为线图生成了一个工作演示:
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, show
# fetch and clear the document
from bokeh.io import curdoc
curdoc().clear()
x = [x*0.005 for x in range(0, 100)]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = Figure(plot_width=400, plot_height=400)
plot.line(x='x', y='y', source=source)
def callback(source=source, window=None):
data = source.data
f = cb_obj.value
x, y = data['x'], data['y']
for i in range(len(x)):
y[i] = window.Math.pow(x[i], f)
source.trigger('change')
slider = Slider(start=0.1, end=4, value=1, step=.1, title="Start week",
callback=CustomJS.from_py_func(callback))
layout = column(slider, plot)
show(layout)
It looks like this:它看起来像这样:
In this demo, when you adjust the slider and press the 'reset' icon, the plot re-draws itself based on the updated formula for y=f(x).在此演示中,当您调整滑块并按下“重置”图标时,绘图将根据 y=f(x) 的更新公式重新绘制。
However, I want to make a scatter plot that changes, not a line plot.但是,我想制作一个变化的散点图,而不是线图。
Problem:问题:
When I simply change plot.line
in above code to plot.circle
, the plot renders okay but it is static - it does not change when you shift the slider and press 'reset'.当我简单地改变plot.line
在上面的代码plot.circle
,情节渲染好的,但它是静态的-当你转移滑块,然后按“复位”它不会改变。 No error messages that I can see.没有我可以看到的错误消息。
I found the answer in the documentation .我在文档中找到了答案。
The final line in callback
should be source.change.emit()
not source.trigger('change')
. callback
的最后一行应该是source.change.emit()
而不是source.trigger('change')
。 I do not know the difference between these two but the latter works with circle plots.我不知道这两者之间的区别,但后者适用于圆形图。
Ie IE
def callback(source=source, window=None):
data = source.data
f = cb_obj.value
x, y = data['x'], data['y']
for i in range(len(x)):
y[i] = window.Math.pow(x[i], f)
source.change.emit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.