Plotting time-series: putting it all together
In this exercise, you will plot two time-series with different scales on the same Axes, and annotate the data from one of these series.
The CO2/temperatures data is provided as a DataFrame called climate_change
. You should also use the function that we have defined before, called plot_timeseries
, which takes an Axes object (as the axes
argument) plots a time-series (provided as x
and y
arguments), sets the labels for the x-axis and y-axis and sets the color for the data, and for the y tick/axis labels:
plot_timeseries(axes, x, y, color, xlabel, ylabel)
Then, you will annotate with text an important time-point in the data: on 2015-10-06, when the temperature first rose to above 1 degree over the average.
This exercise is part of the course
Introduction to Data Visualization with Matplotlib
Exercise instructions
- Use the
plot_timeseries
function to plot CO2 levels against time. Set xlabel to"Time (years)"
ylabel to"CO2 levels"
and color to'blue'
. - Create
ax2
, as a twin of the first Axes. - In
ax2
, plot temperature against time, setting the color ylabel to"Relative temp (Celsius)"
and color to'red'
. - Annotate the data using the
ax2.annotate
method. Place the text">1 degree"
in x=pd.Timestamp('2008-10-06')
, y=-0.2
pointing with a gray thin arrow to x=pd.Timestamp('2015-10-06')
, y =1
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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()