Jaironpz
Jaironpz

Reputation: 23

Empty plot on Bokeh Tutorial Exercise

I'm following the bokeh tutorial and in the basic plotting section, I can't manage to show a plot. I only get the axis. What am I missing?

Here is the code:

df = pd.DataFrame.from_dict(AAPL)
weekapple = df.loc["2000-03-01":"2000-04-01"]
p = figure(x_axis_type="datetime", title="AAPL", plot_height=350, plot_width=800)
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'
p.line(weekapple.date, weekapple.close)
show(p)

I get this:

My result

I'm trying to complete the exercise here (10th Code cell - Exercise with AAPL data) I was able to follow all previous code up to that point correctly.

Thanks in advance!

Upvotes: 2

Views: 2267

Answers (2)

Dmitry Duplyakin
Dmitry Duplyakin

Reputation: 180

In case this is still relevant, this is how you should do you selection:

df = pd.DataFrame.from_dict(AAPL)

# Convert date column in df from strings to the proper datetime format
date_format="%Y-%m-%d"
df["date"] = pd.to_datetime(df['date'], format=date_format)
# Use the same conversion for selected dates
weekapple = df[(df.date>=dt.strptime("2000-03-01", date_format)) & 
               (df.date<=dt.strptime("2000-04-01", date_format))]

p = figure(x_axis_type="datetime", title="AAPL", plot_height=350, plot_width=800)
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'
p.line(weekapple.date, weekapple.close)
show(p)

To make this work, before this code, I have (in my Jupyter notebook):

import numpy  as np
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
import bokeh
import pandas as pd
from datetime import datetime as dt

bokeh.sampledata.download()
from bokeh.sampledata.stocks import AAPL

output_notebook()

As descried at, https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html, .loc is used in operations with the index (or boolean lists); date is not in the index in your dataframe (it is a regular column).

I hope this helps.

Upvotes: 1

bigreddot
bigreddot

Reputation: 34618

You dataframe sub-view is empty:

In [3]: import pandas as pd
   ...: from bokeh.sampledata.stocks import AAPL
   ...: df = pd.DataFrame.from_dict(AAPL)
   ...: weekapple = df.loc["2000-03-01":"2000-04-01"]

In [4]: weekapple
Out[4]:
Empty DataFrame
Columns: [date, open, high, low, close, volume, adj_close]
Index: []

Upvotes: 1

Related Questions