9

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
bigreddot
  • 33,642
  • 5
  • 69
  • 122
RobD
  • 435
  • 4
  • 11
  • 21
  • 3
    Please include your imports as part of a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Mr. T Jul 23 '18 at 18:20
  • What is `type(p)`? Also, please include the full traceback of the `AttributeError` so that we can identify the problem. – Joel Jul 23 '18 at 18:43
  • If this question is about a matplotlib figure, which is created via `pyplot.figure()`, such figure is shown by calling `pyplot.show()`. If this question is about some other object named `Figure`, the question needs to clarify what object that is, via a [mcve]. – ImportanceOfBeingErnest Jul 23 '18 at 19:15
  • you're using Bokeh, not matplotlib, here – Paul H Jul 23 '18 at 19:36
  • Are you in a notebook? If you are in a notebook and want bokeh plots to show inline then it's `output_notebook` that you want to call. – bigreddot Jul 23 '18 at 20:57

4 Answers4

8

Here is the the code I added to the start to get visualization working. I often found that using output_notebook alone will sometimes still not be enough to get show() to display figures as it should. This is a work around using INLINE which has not failed for me yet.

# allows visualisation in notebook
from bokeh.io import output_notebook
from bokeh.resources import INLINE
output_notebook(INLINE)

<your code>

show(p)
Dom McEwen
  • 335
  • 3
  • 9
6

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.

bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • Thanks, its still not working, I even tried adding a circle, it does not throw exceptions but does not display anything either? At this point I'm wondering if I should just go back to matplotlib which I have gotten to work? – RobD Jul 24 '18 at 14:28
  • The `show` function is a thin wrapper around the built-in Python standard libraruy `webbrowser` module. If it is not working that means you have a system configuration issue of some sort, and that Python itself is unable to raise a browser. That's what you need to fix. – bigreddot Jul 24 '18 at 15:03
  • Hello All and bigreddot Ok the fog finally cleared, I needed this output_file("lines.html") – RobD Jul 24 '18 at 15:13
  • Older versions Bokeh required this but it can be omitted with any recent version and a default filename will be used. – bigreddot Dec 29 '20 at 17:17
-1
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()

This should open up the html file in google or edge or whichever is your default browser you have set

-1

Your import is wrong: change "from bokeh.plotting import output_notebook" to "from bokeh.io import output_notebook"