Introduction to NumPy and Matplotlib
Chapter 9: Matplotlib
Bonus task
We will end this lesson with a bonus Matplotlib
task from Josiah!
Task 9: Grand challenge!
You are given a piece of code to plot two lines on the same graph.
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, "r")
plt.show()
Now, try plotting each graph in a different subplot instead, side-by-side on a single Figure
.
Hint: Check out plt.subplots()
or plt.subplot()
.
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)
fig, (axis1, axis2) = plt.subplots(1, 2)
axis1.plot(x, y, "b")
axis2.plot(x, z, "r")
plt.show()