散景教程练习中的空白图

Empty plot on Bokeh Tutorial Exercise

我正在学习散景教程,但在基本绘图部分,我无法显示绘图。我只得到轴。我错过了什么?

代码如下:

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)

我明白了:

My result

我正在尝试完成练习 here(第 10 个代码单元格 - 使用 AAPL 数据练习)我能够正确地遵循之前的所有代码。

提前致谢!

您的数据框子视图为空:

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

如果这仍然相关,这就是您应该如何选择:

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)

为了完成这项工作,在这段代码之前,我有(在我的 Jupyter 笔记本中):

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()

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html所述,.loc用于索引(或布尔列表)的操作; date 不在数据框中的索引中(它是常规列)。

希望对您有所帮助。