Introduction to NumPy and Matplotlib
Chapter 9: Matplotlib
Save your figures
Task 8: Immortalise your figure!
Finally, save your beautiful figure as a .png
file!
See the official documentation on how to do this.
Once you are done, congratulate yourself! You have learnt all the basics of Matplotlib
plotting. 😀 Prepare for your final grand challenge from Josiah on the next page!
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.ylim([-0.5, 2.0])
plt.savefig("my_figure.png")