我正在尝试创建一个回调,该回调在python的bokeh程序包中合并了“月”和“年”滑块。我希望将月份和年份一起使用,以便从数据框的日期时间索引中提取特定信息。当前,正在创建滑块,但不会影响图形中的数据。一些示例代码如下(假定必要的导入):
# Make the ColumnDataSource: source
source = ColumnDataSource(data={
'x' : df.loc['2016-10'].app_total,
'y' : df.loc['2016-10'].app_approval,
'vertical' : df.loc['2016-10'].vertical,
'name' : df.loc['2016-10'].name
})
# Create the figure: plot
plot = figure(title='Application Acceptance Rate 2016-10',
plot_height=400, plot_width=700))
# Add circle glyphs to the plot
plot.circle(x='x', y='y', fill_alpha=.8, source=source)
# Define the callback function: update_plot
def update_plot(attr, old, new):
# set the `yr` name to `slider.value` and `source.data = new_data`
month = pd.to_datetime(slider1.value + '-' + slider2.value)
new_data = {
'x': df.loc[month].app_total,
'y': df.loc[month].app_approval,
'segment': df.loc[month].vertical,
'name': df.loc[month].name,
}
month = pd.to_datetime(slider1.value + '-' + slider2.value)
source.data = new_data
# Add title to figure: plot.title.text
plot.title.text = 'Application Acceptance Rate %Y-%m-%d' % month
# Make slider objects: slider1, slider2
slider1 = Slider(title="Year", start = 2016, end=2018, value= 2016,
step= 1)
slider2 = Slider(title = 'Month', start = 1, end = 12, value = 10, step
= 1)
# Attach the callback to the 'value' property of slider
slider1.on_change('value', update_plot)
slider2.on_change('value', update_plot)
# Make a row layout of widgetbox(slider1, slider2) and plot and add it
to the current document
layout = row(widgetbox(slider1, slider2), plot)
curdoc().add_root(layout)