CDSView.filters更改时如何自动更新x_range?

时间:2019-04-30 06:18:28

标签: python-3.x plot bokeh

我正在尝试使用bokeh来显示CDSView的一些交互式图,我希望在更改CDSView.filters而不更改源时自动更改x_range。但是,无论我如何更改过滤器,x_range都不会改变,并且x_range始终通过源数据范围保持最大范围。 这是一些jupyter笔记本代码...

demo picture

非常感谢您!

from ipywidgets import widgets
from bokeh.io import output_notebook, show, push_notebook, output_file
from bokeh.plotting import figure
from bokeh.layouts import gridplot, column
from bokeh.models import ColumnDataSource, CDSView, IndexFilter

output_notebook()

periods = 500
x = np.linspace(0, 10, periods)
date = pd.date_range('20100101', periods=periods)
sin = np.sin(x) * x
cos = np.cos(x) * x
source = ColumnDataSource(dict(date=date, sin=sin, cos=cos))
view = CDSView(source=source, filters=[IndexFilter(list(range(100, 200)))])

f_sin = figure(plot_width=800, plot_height=200, x_axis_type='datetime')
f_sin.line(x='date', y='sin', view=view, source=source)
f_sin.xaxis[0].visible = False

f_cos = figure(plot_width=800, plot_height=200, x_range=f_sin.x_range, x_axis_type='datetime')
f_cos.line(x='date', y='cos', view=view, source=source)

grid = gridplot([[f_sin], [f_cos]], toolbar_location='right', plot_width=800, plot_height=200)
handle = show(grid, notebook_handle=True)

def update(idx=100):
    idxes = list(range(max(idx-50, 0), idx+1))
    view.filters = [IndexFilter(idxes)]
    # f_sin.x_range.update(start=date[idxes[0]], end=date[idxes[-1]])
    push_notebook()
slider = widgets.SelectionSlider(default=50, step=1, options=[(t.strftime('%Y-%m-%d'), i) for i, t in enumerate(date)])
play = widgets.Play(min=0, max=periods, step=1)
widgets.jslink((play, 'value'), (slider, 'index'))
widgets.interactive_output(update, dict(idx=slider))
display(widgets.HBox([play, slider]))

0 个答案:

没有答案