有没有一种方法可以使用Dropdown小部件在Bokeh上更新绘图设置?

时间:2020-05-12 00:19:31

标签: plot widget bokeh

有人知道在bokeh中使用下拉菜单更改情节设置的方法吗?我真的不想更改数据,这是我通常看到的。我想使轴的比例成为对数。

这是我到目前为止的相关内容:

p3 = figure(y_axis_label='The scale is {}'.format(selected_scale), y_axis_type=selected_scale)

p3.line( x='date', y='value', source = src, legend_label='a label',muted_color=color[k], muted_alpha=0.1, alpha=.9)

def button_callback():
    sys.exit()  # Stop the server
# Button to stop the server
exit_button = Button(label="Stop", button_type="warning", background='red', width=50)
exit_button.on_click(button_callback)
# --------------------------------------------------------------------------------


# Scale change
def change_scale(attr, old, new):
    selected_scale = seleccion.value
    p3 = figure(y_axis_label='The scale is {}'.format(selected_scale), y_axis_type=selected_scale)

    print(seleccion.value, new, 'Hello!!!')

selec = Select(title='Scale', options=['log', 'linear'], value='log', width=50)
selec.on_change('value', change_scale)

layout3 = gridplot([[selec], [p3], [exit_button]])

curdoc().add_root(layout3)

然后从Jupyter笔记本运行

!bokeh serve --show .\COVID19Bokeh.ipynb --port 5001

这确实会在浏览器上打开带有正确图形的新选项卡,但不会像我期望的那样更改p3的设置。

实际上,我回到jupyter笔记本后(关闭选项卡之后),我确实从print(seleccion.value, new, 'Hello!!!')语句获得了所有打印件,但没有任何变化...

1 个答案:

答案 0 :(得分:0)

通过在回调中使用p3 = figure(...)而不实际使用p3,您只是在创建一个全新的情节,然后不使用就将其丢弃。

在Bokeh中,如果要更改某些内容,通常应该直接更改模型。有时,请替换父模型的子模型。

话虽这么说,尤其是轴类型更改并不是一个很好的支持,因此您可能会遇到一些问题,这些问题需要比预期更多的代码。与讨论相关的一些链接:

我从未测试过的想法-您可以尝试创建两个轴并通过更改其visible属性来在两个轴之间切换。