我正在尝试根据发布的简单示例代码在 Bokeh 中绘制散点图 here .
以下代码生成了一个线图的工作演示:
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)
看起来像这样:
在此演示中,当您调整 slider 并按下“重置”图标时,绘图会根据 y=f(x) 的更新公式重新绘制。
但是,我想制作一个会变化的散点图,而不是线图。
问题:
当我简单地将上面代码中的 plot.line
更改为 plot.circle
时,绘图呈现正常但它是静态的 - 当您移动 slider 时它不会改变然后按“重置”。没有我能看到的错误消息。
最佳答案
我找到了答案 in the documentation .
callback
中的最后一行应该是 source.change.emit()
而不是 source.trigger('change')
。我不知道这两者之间的区别,但后者适用于圆图。
即
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()
关于python - 如何使 Bokeh 散点图具有交互性(带 slider ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488701/