2016-02-05 112 views
2

我想將x軸範圍更改爲jupyter中的繪圖更新的一部分。在Jupyter筆記本中嵌入散景活圖中設置x_axis_limit

用於繪製時間序列(線是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軸限制爲預期。我試過How can I accomplish `set_xlim` or `set_ylim` in Bokeh? 但它並沒有實際更新我的情節。一種選擇是切片繪製數據,但是我希望所有數據在用戶縮小時都可用。

回答

2

這讓我花了一點時間來弄清楚你所做的事似乎是合理的! (我已經在郵件列表上問過這個問題,以便更好地理解)。

使其工作是相當直接的,短期的變化

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