python - Set x_axis_limit in Bokeh live plot embedded in Jupyter notebook -
i want change x axis range part of plot update in jupyter.
my update function plotting time series (line instance of multi_line
):
def update_plot(meta, data, fig, line, window_length=3.0): fs = meta["format"]["sample rate"] data = np.asarray(data).transpose()[4:8] x, y = dsp.time_series(data, fs) x = np.tile(x, (y.shape[0], 1)) line.data_source.data['xs'] = x.tolist() line.data_source.data['ys'] = y.tolist() if x.max() >= window_length: fig.x_range = range1d(x.max() - window_length, x.max()) push_notebook()
however, while updates plot new data, not set x axis limits expected. i've tried how can accomplish `set_xlim` or `set_ylim` in bokeh? doesn't update plot. 1 option slice plotted data, want data available if user zooms out.
this took me little while figure out doing seemed reasonable! (i've asked question on mailing list understand better).
making work pretty straight forward, in short change
fig.x_range = range1d(x.max() - window_length, x.max()) push_notebook()
to
fig.x_range.start = x.max() - window_length fig.x_range.end = x.max() push_notebook()
here's complete working example:
from ipywidgets import interact import numpy np bokeh.io import push_notebook bokeh.plotting import figure, show, output_notebook x = np.linspace(0, 2*np.pi, 2000) y = np.sin(x) output_notebook() p = figure(plot_height=300, plot_width=600, y_range=(-5,5)) p.line(x, y, color="#2222aa", line_width=3) def update(range_max=6): p.x_range.end = range_max push_notebook() show(p) interact(update, range_max=(1,10))
the notebook here: http://nbviewer.jupyter.org/github/birdsarah/bokeh-miscellany/blob/master/how%20can%20i%20accomplish%20%60set_xlim%60%20or%20%60set_ylim%60%20in%20bokeh%3f.ipynb
Comments
Post a Comment