Using a plotting function
Defining functions allows us to reuse the same code without having to repeat all of it. Programmers sometimes say "Don't repeat yourself".
In the previous exercise, you defined a function called plot_timeseries
:
plot_timeseries(axes, x, y, color, xlabel, ylabel)
that takes an Axes object (as the argument axes
), time-series data (as x
and y
arguments) the name of a color (as a string, provided as the color
argument) and x-axis and y-axis labels (as xlabel
and ylabel
arguments). In this exercise, the function plot_timeseries
is already defined and provided to you.
Use this function to plot the climate_change
time-series data, provided as a pandas DataFrame object that has a DateTimeIndex with the dates of the measurements and co2
and relative_temp
columns.
This exercise is part of the course
Introduction to Data Visualization with Matplotlib
Exercise instructions
- In the provided
ax
object, use the functionplot_timeseries
to plot the"co2"
column in blue, with the x-axis label"Time (years)"
and y-axis label"CO2 levels"
. - Use the
ax.twinx
method to add an Axes object to the figure that shares the x-axis withax
. - Use the function
plot_timeseries
to add the data in the"relative_temp"
column in red to the twin Axes object, with the x-axis label"Time (years)"
and y-axis label"Relative temperature (Celsius)"
.
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
____(____, ____, ____, "blue", ____, ____)
# Create a twin Axes object that shares the x-axis
ax2 = ____
# Plot the relative temperature data in red
____(____, ____, ____, "red", ____, ____)
plt.show()