2

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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! this was a lot of help.
Glad it helped. Please mark the answer as the solution.
1

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: []

2 Comments

mmm... So, is there something wrong with the df.loc? I'm trying to take a subset of AAPL corresponding to the expressed interval. Any suggestion in that regard? Thanks.
Sorry I am not a Pandas expert, I can only confirm that the df being empty is the answer to the question of why the plot is empty. Perhaps something in this answers the pandas question: stackoverflow.com/questions/29370057/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.