在 jupyter notebook 中使用散景小部件更新散景图 [英] updating bokeh plot with a bokeh widget in jupyter notebook

查看:38
本文介绍了在 jupyter notebook 中使用散景小部件更新散景图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 jupyter notebook 中的散景小部件来更新散景图.我的(有点 hacky)代码如下所示:

I want to use bokeh widgets from within a jupyter notebook to update a bokeh plot. My (somewhat hacky) code looks like this:

from bokeh.plotting import figure
from bokeh.io import output_notebook, push_notebook, show
from bokeh.models import CustomJS, Slider

output_notebook()

power = 0.5
x = [1,2,3]
y = [i**power for i in x]

fig = figure()
plt = fig.circle(x, y)

def update_plot(power):
    x = plt.data_source.data['x']
    plt.data_source.data['y'] = [i**power for i in x]
    push_notebook(handle=bokeh_handle)  


bokeh_handle = show(fig, notebook_handle=True)

##### new notebook cell #####

callback = CustomJS(code="""
if (IPython.notebook.kernel !== undefined) {
    var kernel = IPython.notebook.kernel;
    cmd = "update_plot(" + cb_obj.value + ")";
    kernel.execute(cmd, {}, {});
}
""")

slider = Slider(start=0.1, 
                end=1,
                value=1,
                step=.05,
                title="power",
                callback=callback)
show(slider)

思路是滑块的JS回调调用python函数update_plot(),改变散景图的数据,然后触发push_notebook().

The idea is that the JS callback for the slider calls the python function update_plot(), which changes the data of the bokeh plot and then triggers a push_notebook().

但是,当我移动滑块时,绘图没有更新,但是 出现了一些奇怪的字形在左上角(见红色箭头).

However, when I move the slider, the plot is not updated, but some weird glyphs appear in the upper left corner (see red arrow).

执行 print(plt.data_source.data['y']) 向我展示了回调和 update_plot() 实际上是在滑块移动时调用的.为什么剧情没有正确更新?还是我在这里遗漏了什么?

Executing print(plt.data_source.data['y']) showed me that the callback and update_plot() were actually called upon slider movement. Why is the plot not properly updated? Or am I missing something here?

(我知道我可以使用 ipywidgets.interact 做同样的事情,但我想坚持使用散景小部件.)

(I know that I can do the same thing using ipywidgets.interact, but I want to stick to bokeh widgets.)

推荐答案

我通过在 bokeh.layouts.row 布局中显示图形和滑块小部件来按预期更新绘图:

I got the plot to update as expected by displaying the figure and the slider widget within a bokeh.layouts.row layout:

from bokeh.plotting import figure
from bokeh.io import output_notebook, push_notebook, show
from bokeh.models import CustomJS, Slider
from bokeh.layouts import row

output_notebook()

power = 0.5
x = [1,2,3]
y = [i**power for i in x]

fig = figure()
plt = fig.circle(x, y)

def update_plot(power):
    x = plt.data_source.data['x']
    plt.data_source.data['y'] = [i**power for i in x]
    push_notebook(handle=bokeh_handle)  


##### new notebook cell #####

callback = CustomJS(code="""
if (IPython.notebook.kernel !== undefined) {
    var kernel = IPython.notebook.kernel;
    cmd = "update_plot(" + cb_obj.value + ")";
    kernel.execute(cmd, {}, {});
}
""")

slider = Slider(start=0.1, 
                end=1,
                value=1,
                step=.05,
                title="power",
                callback=callback)
bokeh_handle = show(row(fig, slider), notebook_handle=True)

这篇关于在 jupyter notebook 中使用散景小部件更新散景图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆