0
嘗試在散景中繪製2條線並基於來自2個滑塊的輸入同時更新它們。當我使用multi_line時,行不會更新。與散景中的多線交互
使用IPython的筆記本:
%matplotlib inline
import numpy as np
from ipywidgets import interact
from bokeh.models import Line, ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
定義2個簡單的函數,給我2號線在相同的x值:
def mu(q,tau,c):
p = np.maximum((tau-2*q-c),0)
return p*q
def mu_d(q,tau,c):
mask = (tau-q-c)<0
payoff = .25*(tau-q-c)**2
payoff[mask]=0
return payoff
現在使背景虛化的情節:
x = np.linspace(0, .6, 200)
y = mu(x,1,.1)
y1 = mu_d(x,1,.1)
output_notebook()
source = ColumnDataSource(data=dict(x=x, y=y, y1=y1))
p = figure(title="simple line example", plot_height=300, plot_width=600, y_range=(0, .25))
# p.line(x, y, source=source, alpha=.5, color="red", line_width=2) #this case works
# p.line(x, y1, source=source, alpha=.5, color="red", line_width=2) #this case does not
p.multi_line([x,x], [y,y1], source=source, alpha=.5, color=["red","blue"], line_width=2) # neither does this
def update(tau=1, c=.1):
source.data['y'] = mu(x,tau,c)
source.data['y1'] = mu_d(x,tau,c)
source.push_notebook()
show(p)
而滑塊:
interact(update, tau=(0,1, 0.1), c=(0,.5, 0.1))
要調試的東西,我一直在玩單線圖。事情工作正常,如果我只繪製(x,y),如第一種情況(註釋掉)。當我嘗試第二種情況時,即使當我重新啓動內核並將未使用的source.data
行(錯誤?)註釋掉時,行(x,y1)在滑塊更新時變成(x,y)。我已經在update
命令中打印了mu_d()
輸出,並且其中的內容看起來不錯,但它們並未在圖中更新。
其他人遇到過這個?所有工具(ipython,bokeh等)都是Anaconda dist中的最新版本。