开始使用免费开始使用

使用时间索引进行放大查看

当时间序列使用时间索引表示时,我们可以在绘图时将该索引用作 x 轴。我们也可以使用 pandas 的索引功能选择一段日期范围,从而对时间序列中的特定时期进行放大查看。在本练习中,您将选择时间序列数据集的一部分并绘制该时间段。

要使用的数据存储在名为 climate_change 的 DataFrame 中,它的时间索引包含测量日期,并有两个数据列:"co2""relative_temp"

本练习是课程的一部分

Matplotlib 数据可视化入门

查看课程

练习说明

  • 使用 plt.subplots 创建一个 Figure 和一个 Axes,分别命名为 figax
  • 创建名为 seventies 的变量,包含从 "1970-01-01""1979-12-31" 的所有数据。
  • seventies 的数据添加到图中:使用 DataFrame 的 index 作为 x 值,"co2" 列作为 y 值。

交互式实操练习

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

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
____
编辑并运行代码