開始使用免費開始

使用時間索引放大檢視

當時間序列以時間索引表示時,繪圖時可以將這個索引用作 x 軸。我們也能利用 pandas 的索引功能,選取某個日期範圍,放大檢視時間序列中的特定時段。在本練習中,你將選取時間序列資料集的一部分,並繪製該時段。

要使用的資料存放在名為 climate_change 的 DataFrame 中,使用量測日期作為時間索引,並含有兩個資料欄:"co2""relative_temp"

本練習屬於課程

Matplotlib 資料視覺化入門

檢視課程

練習說明

  • 使用 plt.subplots 建立一個 Figure 與一個 Axes,分別命名為 figax
  • 建立名為 seventies 的變數,包含 "1970-01-01""1979-12-31" 之間的所有資料。
  • seventies 的資料加入圖表:x 值使用 DataFrame 的 index,y 值使用 "co2" 欄位。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

import matplotlib.pyplot as plt

# Use plt.subplots to create fig and ax
____

# Create variable seventies with data from "1970-01-01" to "1979-12-31"
seventies = climate_change[____]

# Add the time-series for "co2" data from seventies to the plot
____(____, ____["co2"])

# Show the figure
____
編輯並執行程式碼