開始使用免費開始

繪製時間序列資料

要繪製時間序列資料,可以使用 Axes 物件的 plot 指令。這個方法的第一個引數是 x 軸的數值,第二個引數是 y 軸的數值。

本練習提供了一個名為 climate_change 的 DataFrame。這個變數使用量測日期作為時間索引,並有兩個資料欄:"co2""relative_temp"

在這個例子中,我們會把 DataFrame 的索引用作 x 軸數值,並將 "relative_temp" 欄位中的數值繪製為 y 軸數值。我們也會為 x 軸與 y 軸加上合適的標籤。

本練習屬於課程

Matplotlib 資料視覺化入門

檢視課程

練習說明

  • climate_change 的資料加入圖中:x 值使用 DataFrame 的 index,y 值使用 "relative_temp" 欄位。
  • 將 x 軸標籤設為 'Time'。
  • 將 y 軸標籤設為 'Relative temperature (Celsius)'。
  • 顯示圖形。

動手互動練習

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

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

# Add the time-series for "relative_temp" to the plot
____

# Set the x-axis label
____

# Set the y-axis label
____

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