This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 9: Matplotlib

Warming up

face William Hunter Josiah Wang

Here is a series of exercises to practice plotting with Matplotlib.

These exercises were prepared by William, a Teaching Scholar.

Your task is to reproduce the figures as provided.

Each exercise builds on your solutions from the previous task. You will only need to add one or two new lines to your existing solution.

Ready?

Firstly, remember to import numpy and matplotlib so that you can use them!

import numpy as np
import matplotlib.pyplot as plt

We will also define our x, y, and z arrays to be plotted.

resolution = 1000
x = np.linspace(0, 50, resolution)
y = np.sin(x)
z = np.exp(y)

Task 1: Warming up

Let’s do this one together! Plot y against x and call this figure 1.

plt.figure(1)
plt.plot(x, y)
plt.show()

Line graph