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

Chapter 9: Matplotlib

Legends

face William Hunter Josiah Wang

Task 6: You are a legend!

Add a legend onto the graph to make sure you can tell which is y and z. Place the legend in the top centre of the figure.

Add a legend

See this documentation on how to do this!

import numpy as np
import matplotlib.pyplot as plt

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

plt.figure(1)
plt.plot(x, y, "b")
plt.plot(x, z, color="r")
plt.legend(["y", "z"], loc="upper center")
plt.title("My Graph")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude (W)")
plt.show()