Exercise

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.

Instructions

100 XP
  • 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 and ylabel, setting the y-axis label color using color.
  • Set the y-axis tick parameters using the tick_params method of the Axes object, setting the colors key-word to color.