Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bokeh plot not showing with show(p) or p.show()

Tags:

python

bokeh

Trying to get a Bokeh chart to show with this code, I either get nothing if I use show(p) or

AttributeError: 'Figure' object has no attribute 'show'

How can I fix this?

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
like image 728
RobD Avatar asked Jul 23 '18 18:07

RobD


1 Answers

There is no show method on plots in Bokeh, and never has been. There is a show function that you can pass plots (or layouts of plots and widgets) to.

from bokeh.io import output_file, show
from bokeh.plotting import figure

p = figure(...)
p.circle(...)

output_file("foo.html")

show(p)

This is the very first thing explained in the Quickstart: Getting Started section and this pattern is also repeated in hundreds of examples throughout the docs.

like image 71
bigreddot Avatar answered Nov 02 '22 10:11

bigreddot