Exercise Notebook 5 - Figures#

Note

This week’s Notebook has theory together with the exercise. Download it by clicking the download button() in the top right and selecting “Exercise notebook”.

5.1 Plotting with Matplotlib#

In this notebook we will learn how to make figures in Python, using the matplotlib.pyplot module. We start by importing the module.

import matplotlib.pyplot as plt
# this imports matplotlib.pyplot, and makes it's content's accessible through plt, for example: plt.plot
# the name plt is arbitrary but commonly used

Let’s create some data which we would like to plot.

x = [1, 2, 3, 4, 5]
y = [1, 5, 2, 3, 2]
plt.figure()     # start a new plot
plt.plot(x, y)   # add data to the plot
plt.show()       # we're done setting up the plot, show it                 

This is our first plot. Can you see how the data is shown in the figure?

In notebooks, the plt.show() step is not needed, we will omit it below. However, if you run Python code outside a notebook, you need to include plt.show(), otherwise you never see the plot. Also plt.figure() can be omitted in notebooks, if you create only a single figure per cell in the notebook. Outside notebooks it cannot be omitted.

A good plot needs labels describing the axes and their units:

plt.plot(x, y)
plt.xlabel('Time since start of experiment (days)')
plt.ylabel('Daily apple consumption')
plt.title('Experiment #4')
Exercise 5.1.1

What if your data does not represent a curve, but are just points in x-y space? For example in a scatter-plot. Plot the data from above, using the function plt.scatter().

5.1.2 Multiple curves, legends#

Let’s make some temperature plots. First, run the cell below to define data to plot.

month=range(1,13)
T_NL      = [  3.7,   3.9,  6.5,  9.5, 13.1, 15.8, 17.9, 17.8, 14.8, 11.1,  7.2,  4.6]
T_Madrid  = [  3.8,   4.4,  7.2,  9.2, 13.4, 18.6, 22.2, 22.0, 17.7, 12.3,  6.9,  4.3]
T_Lapland = [-12.4, -11.4, -6.6, -0.2,  6.3, 12.3, 15.5, 13.0,  7.8,  0.7, -5.8, -9.6] # Sodankylä, Finland
# mean temperature per month, °C, 1990 - 2019
# Data from https://climatecharts.net/
Exercise 5.1.2

  • Create plots of the three temperature time series. Just do three plt.plot() calls after each other. Like this: plt.plot(month, T_NL)

You see that Matplotlib selects different colors for the three curves. But which is which? You need a legend.

  • add a label argument to each plot call: plt.plot(month, T_NL, label='Madrid'). Then add the command plt.legend() at the end of your script to create a legend which shows which colors and labels belong together.

  • add axis labels and units.

# your plotting code here

Optional arguments to functions#

This is the first time we have used a construction like label='Madrid' in a function call. This is called a keyword argument. Using keyword arguments makes it possible to call a function with many optional arguments, and specifying only the arguments you need. What it means is that you pass the string 'Madrid' to the label argument of the function. Many of the matplotlib functions have many optional arguments.

Documentation#

Matplotlib is large and capable, but it can be hard to know which functions in it to use to get the effect one wants. There is a lot of documentation on the project homepage https://matplotlib.org. The detailed function description can be quite hard to read, but the examples are helpful.

A good starting point in the matplotlib documentation is the getting started guide. Another good resource is the gallery of plot types with example code to create them.

You can try a web search, for “matplotlib” and what you want to achieve. Often you find results from stackoverflow which tend to be helpful, or links to the official documentation (can be good or too complicated). Then there tends to be a lot of low-quality, advertising-infested data science blogs which are less useful.

Exercise 5.1.3 - Plotting in style

In the Matplotlib documentation (see above), find out how you can set the line style (solid line, dashed line, dotted line, …) and line color in a plot. Copy the temperature plot above, and choose line styles and line colors for the curves.

5.1.3 Plotting in 3D#

Example: Parametric curve in 3D#

import math

ax = plt.axes(projection='3d')
# An 'axes' object, in which we plot. 
# Previously these were automatically created for us by plt.plot(). 
# We need to do it ourselves now to be able to set the 3d projection.

x = [] # empty lists for x,y,z coordinates
y = []
z = []

for i in range(600):
    # each iteration, add one point to the lists
    t = i/300
    x.append(t)
    y.append(math.sin(7*t))
    z.append(math.cos(7.8*t))

ax.plot3D(x,y,z)
Exercise 5.1.4

Copy the parametric curve example above, modify it to draw something else. Color the curve in your favourite color. Add labels to the x, y, and z axes.

Label hint: unfortunately there is no function plot.zlabel(). Instead you have to use ax.set_zlabel('...'). You can use ax.set_xlabel() and ax.set_ylabel() too. Your script looks nicer if you handle all axes in the same way.

Note: the z-label doesn’t necessarily show up anyway, perhaps it’s hidden behind the plot. If you could rotate the plot it might show up. For now let’s choose our battles and not worry about this.

Exercise 5.1.5 - Bar graph

In the Matplotlib documentation, find out how to make bar graphs. Plot the monthly mean rainfall data below in a bar graph. Add labels on the axes.

Hint: you can plot the different locations one by one, one plot command each.

For now, let’s accept that the bars sometimes overlap and hide each other. (One can in principle shift the bars in x so that they don’t overlap).

# just run this cell to define the data

# Monthly mean precipitation (mm)
# Data from https://climatecharts.net/

month = range(1,13)
Pr_NL      = [72.8, 54.1, 52.5, 38.7, 50.0, 63.0, 73.1, 82.9, 82.9, 86.8, 87.7, 83.6] # Netherlands
Pr_Madrid  = [43.0, 45.1, 44.8, 58.6, 60.7, 31.0, 12.7, 16.3, 32.5, 75.8, 60.7, 48.5] # Spain, around Madrid
Pr_Lapland = [27.0, 25.9, 22.5, 22.1, 35.6, 60.0, 66.9, 58.1, 46.6, 42.9, 34.2, 28.4] # Finland, Lapland, around Sodankylä
#             Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec   
# your plotting code here