尝试使用此代码显示散景图,如果我使用show(p)或
AttributeError: 'Figure' object has no attribute 'show'
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
from math import pi
import pandas as pd
from bokeh.plotting import figure, show, output_notebook
from bokeh.models.annotations import Title
from nsepy import get_history
from datetime import date
from datetime import datetime
from pykalman import KalmanFilter
df = get_history(symbol="TCS", start = date(2018,1,1),end = date(2018,7,22))
print(df)
kf = KalmanFilter(transition_matrices = [1],
observation_matrices = [1],
initial_state_mean = df['Close'].values[0],
initial_state_covariance = 1,
observation_covariance=1,
transition_covariance=.01)
state_means,_ = kf.filter(df[['Close']].values)
state_means = state_means.flatten()
df["date"] = pd.to_datetime(df.index)
mids = (df.Open + df.Close)/2
spans = abs(df.Close-df.Open)
inc = df.Close > df.Open
dec = df.Open > df.Close
w = 12*60*60*1000 # half day in ms
output_notebook()
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
#This causes an exception tol with p.show() no show in figure
p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, toolbar_location="left",y_axis_label = "Price",
x_axis_label = "Date")
p.segment(df.date, df.High, df.date, df.Low, color="black")
p.rect(df.date[inc], mids[inc], w, spans[inc], fill_color='green', line_color="green")
p.rect(df.date[dec], mids[dec], w, spans[dec], fill_color='red', line_color="red")
p.line(df.date,state_means,line_width=1,line_color = 'blue',legend="Kalman filter")
t = Title()
t.text = 'Kalman Filter Estimation'
p.title = t
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha=0.3
p.show() #Throws attribute error show does not exist
#show(p) #Nothing happens on this
Run Code Online (Sandbox Code Playgroud)
这是我添加到开始部分以使可视化工作的代码。我经常发现,单独使用output_notebook有时仍然不足以让 show() 按应有的方式显示数字。这是一个使用INLINE 的解决方案,对我来说还没有失败。
# allows visualisation in notebook
from bokeh.io import output_notebook
from bokeh.resources import INLINE
output_notebook(INLINE)
<your code>
show(p)
Run Code Online (Sandbox Code Playgroud)
Bokeh 中的绘图没有任何show方法,而且从来没有。有一个show函数可以将绘图(或绘图和小部件的布局)传递给它。
from bokeh.io import output_file, show
from bokeh.plotting import figure
p = figure(...)
p.circle(...)
output_file("foo.html")
show(p)
Run Code Online (Sandbox Code Playgroud)
这是快速入门:入门部分中解释的第一件事,并且在整个文档的数百个示例中也重复了此模式。