开始使用免费开始使用

可视化股票价格趋势

Google Finance 已弃用其 API,但 DataReader 现在提供了数据源 'iex'。若要在 DataCamp 环境之外实验数据,您需要一个 IEX Cloud 账户。

功能上最重要的变化是数据被限制为最近 5 年。DataReader 返回的 DataFrame 具有相同的 columns,但为小写。

在 Python 中可视化股票价格趋势,离不开 matplotlib.pyplot 包。

在本练习中,您将导入 Facebook 在 2016 年的股票价格数据,并绘制其整个期间的收盘价!DataReaderdate 已经导入。

本练习是课程的一部分

在 Python 中导入与管理金融数据

查看课程

练习说明

  • matplotlib.pyplotplt 的别名导入。
  • 使用 date(),分别将 startend 日期设为 2016 年 1 月 1 日和 2016 年 12 月 31 日。
  • ticker 设为 Facebook 的股票代码 'FB'data_source 设为 'iex'
  • 创建一个 DataReader() 对象以导入股票价格,并赋值给 stock_prices
  • 绘制 stock_prices 中的 'close' 数据,将 ticker 设为标题,并显示结果。

交互式实操练习

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

# Import matplotlib.pyplot


# Set start and end dates
start = ____
end = ____

# Set the ticker and data_source
ticker = ____
data_source = ____

# Import the data using DataReader
stock_prices = ____

# Plot close
____

# Show the plot
plt.show()
编辑并运行代码