开始使用免费开始使用

绘制时间序列数据

要绘制时间序列数据,我们使用 Axes 对象的 plot 命令。该方法的第 1 个参数是 x 轴的取值,第 2 个参数是 y 轴的取值。

本练习提供了一个名为 climate_change 的 DataFrame。该变量使用测量日期作为时间索引,并包含两个数据列:"co2""relative_temp"

在这里,我们将使用 DataFrame 的索引作为 x 轴取值,并将 "relative_temp" 列中的数值绘制为 y 轴取值。同时为 x 轴和 y 轴添加合适的标签。

本练习是课程的一部分

Matplotlib 数据可视化入门

查看课程

练习说明

  • climate_change 中的数据添加到图中:使用 DataFrame 的 index 作为 x 值,使用 "relative_temp" 列作为 y 值。
  • 将 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
____
编辑并运行代码