尝试使用此代码显示散景图,如果使用show(p)
或
AttributeError: 'Figure' object has no attribute 'show'
我该如何解决?
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
答案 0 :(得分:1)
散景中的地块上没有show
方法,而且从来没有。有一个show
函数,您可以将图(或图和小部件的布局)传递到该图。
from bokeh.io import output_file, show
from bokeh.plotting import figure
p = figure(...)
p.circle(...)
output_file("foo.html")
show(p)
这是Quickstart: Getting Started部分中解释的第一件事,并且在整个文档的数百个示例中也重复了这种模式。
答案 1 :(得分:1)
这是我为使可视化正常工作而添加的代码。我经常发现,仅使用 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)
答案 2 :(得分:0)
from math import pi
import pandas as pd
from bokeh.plotting import figure, show, output_file
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()
#please note in the import statement above, I have changed it from
output_notebook to output_file
output_file=("TCS.html", title = "Kalman Filter Estimation", mode="cdn")
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()
这应该在google或edge或您设置的默认浏览器中打开html文件
答案 3 :(得分:-1)
您的导入错误: 将“从bokeh.plotting导入output_notebook”更改为“从bokeh.io导入output_notebook”