python - 如何使 Bokeh 散点图具有交互性(带 slider )

标签 python plot slider bokeh

我正在尝试根据发布的简单示例代码在 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)

看起来像这样:

Bokeh plot screenshot

在此演示中,当您调整 slider 并按下“重置”图标时,绘图会根据 y=f(x) 的更新公式重新绘制。

但是,我想制作一个会变化的散点图,而不是线图。

问题:

当我简单地将上面代码中的 plot.line 更改为 plot.circle 时,绘图呈现正常但它是静态的 - 当您移动 slider 时它不会改变然后按“重置”。没有我能看到的错误消息。

enter image description here

最佳答案

我找到了答案 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/

相关文章:

R ggplot2 ggrepel - 在了解所有点的同时标记点的子集

python - 如何在 Seaborn 热图单元格中显示多个注释

python - 同一页面中的多个表单,提交时保持数据填充

python - 制作所有可能的对称 4x4 像素图像

r - 调整R中图例的字体

css - jquerymobile 双 slider ,高亮范围

python - PyQt 猴子修补 QLineEdit.paste?

r - 标准化色标,面板中的 persp() 图,R

jquery - 将 slider 调整为全屏,不滚动以其为中心的元素

javascript - 光滑的 slider 拇指不显示