How can I link the slider to update my plot in bokeh while in a jupyter notebook?

左心房为你撑大大i 提交于 2019-12-23 04:47:36

问题


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.

My code is as follows:

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)

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.


回答1:


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)

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.



来源:https://stackoverflow.com/questions/52283912/how-can-i-link-the-slider-to-update-my-plot-in-bokeh-while-in-a-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!