在使用Jupyter笔记本时,如何链接滑块以更新bokeh中的绘图? [英] How can I link the slider to update my plot in bokeh while in a jupyter notebook?

查看:211
本文介绍了在使用Jupyter笔记本时,如何链接滑块以更新bokeh中的绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为ed_montecarlo的函数,该函数以一定的迭代次数运行蒙特卡罗模拟,并将结果作为具有多个列的熊猫数据框返回(此处未全部使用.)目前,我正在尝试将其链接到具有Bokeh的图上,并具有一个滑块,当更改该滑块时,将使用滑块的新值重新运行该函数.

I have created a function called ed_montecarlo that runs a montecarlo simulation with a set number of iterations and returns the results as a pandas data frame with multiple columns (not all of which are used here.) Currently I am trying to link this to a plot with Bokeh and have a slider that when changed will re-run the function using the new value of the slider.

我的代码如下:

def modify_doc(doc):

    source = ColumnDataSource(ed_montecarlo(num=1000))

    TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

    iter_scatter= figure(x_axis_label='Iteration Number', y_axis_label='Margin', title='Scatter Plot of Iterrations',
                         tools=TOOLS, plot_height=400, plot_width=550)

    iter_scatter.yaxis[0].formatter = NumeralTickFormatter(format="$0.00")

    dots = iter_scatter.scatter(x='index', y='Margin', source=source, fill_alpha=.5, line_color=None,
                            hover_fill_color='firebrick', hover_alpha=.9, hover_line_color=None, size=10)

    iter_scatter.line(x='index', y='Median Margin', source=source, line_color='cyan', line_width=5, line_alpha=0.8)

    band = Band(base='index', lower='25th Margin', upper='75th Margin', source=source, level='underlay',
            fill_alpha=0.3, line_width=3, line_alpha=.8, line_color='cyan', fill_color='cyan')

    iter_scatter.add_layout(band)

    iter_scatter.add_tools(HoverTool(tooltips=[('Iterration', '@index'),
                                           ('Margin', '$@Margin{%0.2f}')], 
                                 formatters={'Margin': 'printf',},
                                 renderers = [dots], mode='mouse'))
    def callback(attr, old, new):
        num = iter_slider.value


    iter_slider = Slider(start=100, end=5000, step=100, value=1000, title='Number of Iterations')
    iter_slider.on_change('value', callback) 

    doc.add_root(column(iter_slider, iter_scatter))

show(modify_doc)

当我运行上面的代码时,散点图可以使用1000次迭代正确显示,但是当我移动滑块时,它不会重新运行蒙特卡洛函数并更新图.我想念什么?我已经为此敲了好一会儿.

When I run the above code the scatterplot appears correctly using 1000 iterations, however when I move the slider it will not re run the monte carlo function and update the plot. What am I missing? I have been banging my head for a good while on this.

推荐答案

您的回调没有做任何实际的工作.您将滑块的值分配给局部变量num(完全没有其他影响),然后立即退出回调.如果要更新图,则必须更新数据源.您还没有说过ed_montecarlo返回的类型是什么,但是可能与

Your callback is not doing any actual work. You assign the value of the slider to a local variable num (which has no other affect at all) and then you immediately exit the callback. If you want the plot to update, you have to update the data source. You have not said what the type returned by ed_montecarlo is, but it would be something along the lines of

def callback(attr, old, new):
    source.data = ed_montecarlo(num=iter_slider.value)

假设ed_montecarlo返回一个适当的Python dict.如果不是,则需要将其转换为python dict,并以CDS列名称作为键,将数据数组作为值.

Assuming ed_montecarlo returns an appropriate Python dict. If not, you need to convert it however necessary to be a python dict with the CDS column names as keys and data arrays as values.

这篇关于在使用Jupyter笔记本时,如何链接滑块以更新bokeh中的绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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