使用时间索引进行放大查看
当时间序列使用时间索引表示时,我们可以在绘图时将该索引用作 x 轴。我们也可以使用 pandas 的索引功能选择一段日期范围,从而对时间序列中的特定时期进行放大查看。在本练习中,您将选择时间序列数据集的一部分并绘制该时间段。
要使用的数据存储在名为 climate_change 的 DataFrame 中,它的时间索引包含测量日期,并有两个数据列:"co2" 和 "relative_temp"。
本练习是课程的一部分
Matplotlib 数据可视化入门
练习说明
- 使用
plt.subplots创建一个 Figure 和一个 Axes,分别命名为fig和ax。 - 创建名为
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
____