本文介绍了滑块值不更新Bokeh Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Bokeh在Jupyter Notebook中制作互动情节时遇到了困难。我想绘制一张世界地图,并展示一些数据随时间的发展情况。我成功地绘制了一个曲线图,并使用滑块来调整年份,但当我更改滑块时,滑块值不会更新。滑块的代码如下:

#creating the data source as a dict
source = ColumnDataSource({
    'x': p_df['x'],
    'y': p_df['y'],
    'Country': p_df['Country'],
    'nkill': p_df['nkill']
})

#making a slider and assign the update_plot function to changes
slider = Slider(start=start_yr, end=end_yr, step=1, value=start_yr, title='Year')
slider.on_change('value',update_plot)

#the update_plot function which needs to run based on the new slider.value
def update_plot(attr, old, new):
    #Update glyph locations
    yr = slider.value
    Amountkills_dt_year = p_df[p_df['Year'] ==yr]
    new_data = {
        'x': Amountkills_dt_year['x'],
        'y': Amountkills_dt_year['y'],
        'Country': Amountkills_dt_year['Country'],
        'nkill': Amountkills_dt_year['nkill']
    }
    source.data = new_data
    #Update colors
    color_mapper = LinearColorMapper(palette='Viridis256',
                                 low = min(Amount_of_Terrorist_Attacks['nkill']),
                                 high = max(Amount_of_Terrorist_Attacks['nkill']))
要使用UPDATE_PLOT()函数更新绘图的位置。我尝试了Python bokeh slider not refreshing plot中的解决方案,但仍然出现相同的错误。

推荐答案

像滑块这样的Bokeh窗口小部件在Jupyter笔记本上不能工作(至少,如果不使用一些Java脚本就不能工作)。正如documentation所说:

To use widgets, you must add them to your document and define their functionality. Widgets can be added directly to the document root or nested inside a layout. There are two ways to program a widget’s functionality:

        Use the CustomJS callback (see JavaScript Callbacks). This will work in standalone HTML documents.

        Use bokeh serve to start the Bokeh server and set up event handlers with .on_change (or for some widgets, .on_click).
正如@bigreddot提示的那样,您将需要使用bokeh服务器,否则也许讨论的木星相互作用子的一些特征here

这篇关于滑块值不更新Bokeh Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:51