Defining a function that plots time-series data
Once you realize that a particular section of code that you have written is useful, it is a good idea to define a function that saves that section of code for you, rather than copying it to other parts of your program where you would like to use this code.
Here, we will define a function that takes inputs such as a time variable and some other variable and plots them as x and y inputs. Then, it sets the labels on the x- and y-axis and sets the colors of the y-axis label, the y-axis ticks and the tick labels.
This exercise is part of the course
Introduction to Data Visualization with Matplotlib
Exercise instructions
- Define a function called
plot_timeseries
that takes as input an Axes object (axes
), data (x
,y
), a string with the name of a color and strings for x- and y-axis labels. - Plot y as a function of in the color provided as the input
color
. - Set the x- and y-axis labels using the provided input
xlabel
andylabel
, setting the y-axis label color usingcolor
. - Set the y-axis tick parameters using the
tick_params
method of the Axes object, setting thecolors
key-word tocolor
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define a function called plot_timeseries
def ____(axes, x, y, color, xlabel, ylabel):
# Plot the inputs x,y in the provided color
axes.____(____, ____, color=____)
# Set the x-axis label
____.____(____)
# Set the y-axis label
____.____(____, color=____)
# Set the colors tick params for y-axis
____.____('y', colors=____)