开始使用免费开始使用

为时间序列图添加注释

在图表中添加注释可以突出有趣的信息。例如,在描述气候变化数据集时,您可能想标出相对气温首次超过 1 摄氏度的日期。

为此,我们将使用 Axes 对象的 annotate 方法。在本练习中,名为 climate_changeDataFrame 已加载到内存中。请使用 Axes 方法,仅将相对气温列按日期作图,并为数据添加注释。

本练习是课程的一部分

Matplotlib 数据可视化入门

查看课程

练习说明

  • 使用 ax.plot 方法,将 DataFrame 的索引与 relative_temp 列绘制成图。
  • 使用 annotate 方法,在位置 (pd.Timestamp('2015-10-06'), 1) 添加文本 '>1 degree'

交互式实操练习

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

fig, ax = plt.subplots()

# Plot the relative temperature data
____

# Annotate the date at which temperatures exceeded 1 degree
ax.____(____, ____)

plt.show()
编辑并运行代码