2

I want to change the x axis range as part of a plot update in jupyter.

My update function for plotting a time series (line is an 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 this updates the plot with the new data, it does not actually set the x axis limits as expected. I've tried How can I accomplish `set_xlim` or `set_ylim` in Bokeh? however it doesn't actually update my plot. One option would be to slice the plotted data, however I want all the data to be available if the user zooms out.

Community
  • 1
  • 1
user2561747
  • 1,333
  • 2
  • 16
  • 39

1 Answers1

2

This took me a little while to figure out as what you were doing seemed reasonable! (I've asked this question on the mailing list to understand better).

Making it work is 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 a complete working example:

from ipywidgets import interact
import numpy as np

from bokeh.io import push_notebook
from 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 is 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

birdsarah
  • 1,165
  • 8
  • 20