开始使用免费开始使用

添加一个 Div

这家对冲基金正在寻找线上零售市场的下一匹黑马。他们请您制作一幅图,展示亚马逊在过去几年中的股价表现,并标注该期间内股价高于平均水平的日期。

我们已为您从 stocks 数据集创建了一个子集,筛选了 AMZN 的股票,并将其存为一个 Bokeh 的 source 对象。接下来,您将构建一幅折线图,包含一个 Div 和一个 BoxAnnotation,并使用 layout 进行展示。

本练习是课程的一部分

使用 Bokeh 进行交互式数据可视化

查看课程

练习说明

  • 从相关的 Bokeh 类中导入 layoutDiv
  • 为布局添加标题 "Amazon stock prices vs. average price over the period"
  • 显示包含 Div 的 Bokeh 布局,并在其下方放置该 figure

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import modules
____
____
fig = figure(x_axis_label="Date", y_axis_label="Stock Price ($)")
fig.line(x="date", y="close", source=source, color="purple")
box = BoxAnnotation(bottom=amazon["close"].mean(), fill_color="green", fill_alpha=0.3)
fig.add_layout(box)
fig.xaxis[0].formatter = DatetimeTickFormatter(months="%b %Y")

# Create title
title = ____
output_file(filename="amazon_stocks.html")

# Display layout
____
编辑并运行代码