開始使用免費開始

視覺化股票價格走勢

Google Finance 已淘汰其 API,但 DataReader 現在提供了資料來源 'iex'。若要在 DataCamp 以外的環境實作,你需要一個 IEX Cloud 帳號。

功能上最重要的改變,是資料僅限於最近 5 年。由 DataReader 回傳的 DataFrame 擁有相同的 columns,但全部為小寫。

在 Python 進行股票價格走勢視覺化時,matplotlib.pyplot 套件是關鍵工具。

在本練習中,你將匯入 2016 年 Facebook 的股價資料,並繪製整段期間的收盤價!DataReaderdate 已經匯入完成。

本練習屬於課程

在 Python 中匯入與管理財務資料

檢視課程

練習說明

  • 匯入 matplotlib.pyplot 並命名為 plt
  • 使用 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()
編輯並執行程式碼