【发布时间】:2020-06-28 00:46:31
【问题描述】:
我在 Jupyter Notebook 中使用 Bokeh 制作交互式情节时遇到了困难。我想绘制一张世界地图并显示一些数据随时间的发展。我成功地制作了一个绘图并有一个滑块来调整年份,但是当我更改滑块时,滑块值不会更新。滑块的代码如下:
#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 服务器应用程序运行,即使用
bokeh serve app.py? -
不,我在 jupyter notebook 中运行我的应用程序并使用
show()绘制我的地图,我猜可能使用show()会生成静态图而不是可以与我的滑块交互的图。 .
标签: python python-3.x slider bokeh interactive