在 Jupyter Notebook 中嵌入的 Bokeh 实时图中设置 x_axis_limit

Set x_axis_limit in Bokeh live plot embedded in Jupyter notebook

我想在 jupyter 中更改 x 轴范围作为绘图更新的一部分。

我用于绘制时间序列的更新函数(线是 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()

但是,虽然这会使用新数据更新绘图,但实际上并没有按预期设置 x 轴范围。我试过了 但是它实际上并没有更新我的情节。一种选择是对绘制的数据进行切片,但是我希望在用户缩小时所有数据都可用。

我花了一点时间才弄明白,因为你所做的似乎很合理! (我已经在邮件列表中问过这个问题以便更好地理解)。

让它工作非常简单,简而言之就是改变

fig.x_range = Range1d(x.max() - window_length, x.max())
push_notebook()

fig.x_range.start = x.max() - window_length
fig.x_range.end = x.max()
push_notebook()

这是一个完整的工作示例:

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))

笔记本在这里: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