绘制时间序列:整合应用
在本练习中,您将把两个量纲不同的时间序列绘制在同一个 Axes 上,并对其中一个序列的数据进行标注。
CO2/气温数据以名为 climate_change 的 DataFrame 提供。您还需要使用我们先前定义的函数 plot_timeseries。该函数接收一个 Axes 对象(参数名为 axes),绘制时间序列(由 x 和 y 提供),同时设置 x 轴和 y 轴的标签,并为数据以及 y 轴刻度/轴标签设置颜色:
plot_timeseries(axes, x, y, color, xlabel, ylabel)
随后,您将用文本标注数据中的一个关键时间点:在 2015-10-06,这一天气温首次较长期平均值上升超过 1 度。
本练习是课程的一部分
Matplotlib 数据可视化入门
练习说明
- 使用
plot_timeseries函数将 CO2 水平随时间绘制出来。将 xlabel 设为"Time (years)",ylabel 设为"CO2 levels",color 设为'blue'。 - 创建
ax2,作为第一个 Axes 的"孪生"坐标轴。 - 在
ax2中,将气温随时间绘制出来,将 ylabel 设为"Relative temp (Celsius)",color 设为'red'。 - 使用
ax2.annotate方法进行标注。在 x=pd.Timestamp('2008-10-06')、y=-0.2处放置文本">1 degree",并用一根细灰色箭头指向 x=pd.Timestamp('2015-10-06')、y=1。
交互式实操练习
通过完成这段示例代码来试试这个练习。
fig, ax = plt.subplots()
# Plot the CO2 levels time-series in blue
plot_timeseries(____, ____, ____, 'blue', ____, ____)
# Create an Axes object that shares the x-axis
ax2 = ____
# Plot the relative temperature data in red
plot_timeseries(____, ____, ____, 'red', ____, ____)
# Annotate point with relative temperature >1 degree
ax2.____(">1 degree", ____, ____, ____)
plt.show()