matplotlib is a plotting library for Python and the NumPy library. It is easy to use and can be used to generate scatter or bar plots, density maps and even 3D plots in publication quality.
Press Spacebar
to go to the next slide (or ?
to see all navigation shortcuts)
Lunch Time Python, Scientific Software Center, Heidelberg University
Available via pip:
python -m pip install -U matplotlib
Or install via conda:
conda install matplotlib
Plot the sin and cos over a range of angles. For this, we also need numpy.
import numpy as np
import matplotlib.pyplot as plt
xvals = np.arange(0, 2 * np.pi, 0.1)
plt.plot(xvals, np.sin(xvals))
plt.plot(xvals, np.cos(xvals))
[<matplotlib.lines.Line2D at 0x7f1f60037ca0>]
The default settions already look quite nice!
Or more generally, if you want to suppress the output (return) of the plt()
function.
plt.plot(xvals, np.sin(xvals))
plt.plot(xvals, np.cos(xvals))
plt.show()
plt.show()
closes the plot; if you plot using a script and not a notebook, place one plt.show()
command at the end of your script.
You can plot static images inside your notebook using the %matplotlib inline
magic: You only need to run this once. It is not always necessary to put this, but it makes it clear which Matplotlib backend should be used.
%matplotlib inline
plt.plot(xvals, np.sin(xvals))
plt.plot(xvals, np.cos(xvals))
plt.show()
xvals = np.random.randint(10, size=10)
yvals = np.random.randint(10, size=10)
plt.scatter(xvals, yvals)
plt.show()
xvals = np.linspace(1, 10, 10)
yvals = np.random.randint(10, size=10)
plt.bar(xvals, yvals)
plt.show()
plt.bar(xvals, yvals, label="Random series")
plt.legend()
plt.show()
plt.bar(xvals, yvals, label="Random series")
plt.legend(fontsize=16, loc="upper right")
plt.xlabel("Integer", fontsize=18)
plt.ylabel("Magnitude", fontsize=22, color="red")
plt.title("My custom plot", fontsize=22)
plt.show()
xvals = np.arange(0, 2 * np.pi, 0.1)
plt.plot(xvals, np.sin(xvals), marker="x", markevery=10, color="blue")
plt.plot(xvals, np.cos(xvals), marker="<", color="black", alpha=0.5)
plt.show()
A Figure
in matplotlib is the whole plot (or window in the user interface) and can contain multiple plots. By accessing the Artist layer ("object-based plotting"), you can access more customizing options than with the basic plt.xxx
Scripting layer ("procedural plotting") (see https://matplotlib.org/1.5.1/faq/usage_faq.html#parts-of-a-figure).
This also allows you to include multiple plots in one Figure.
fig = plt.figure(figsize=(12, 10)) # width = 12 inches and height = 10 inches
# create one axes object
ax1 = fig.add_subplot(211) # (2, 1, 1) no of rows, no of columns, no of plots
ax1.plot(xvals, np.sin(xvals), marker="x", color="blue")
plt.show()
It is also possible to use add_axes()
instead of add_subplot()
, but not recommended as with the latter, matplotlib takes care of the exact position of the axes in the figure.
Using add_subplot()
, we can add one axes
object at a time:
fig = plt.figure(figsize=(12, 10))
ax1 = fig.add_subplot(221)
ax1.plot(xvals, np.sin(xvals), marker="x", color="blue")
ax2 = fig.add_subplot(222)
ax2.plot(xvals, np.cos(xvals), marker="x", color="blue")
ax3 = fig.add_subplot(223)
ax3.plot(xvals, np.tan(xvals), marker="x", color="blue")
ax4 = fig.add_subplot(224)
ax4.plot(xvals, np.tanh(xvals), marker="x", color="blue")
plt.show()
fig, ax = plt.subplots(figsize=(12, 10), nrows=2, ncols=2)
ax[0, 0].plot(xvals, np.sin(xvals), marker="x", color="blue")
ax[0, 1].plot(xvals, np.cos(xvals), marker="x", color="blue")
ax[1, 0].plot(xvals, np.tan(xvals), marker="x", color="blue")
ax[1, 1].plot(xvals, np.tanh(xvals), marker="x", color="blue")
plt.savefig("my_figure.pdf", bbox_inches="tight")
plt.savefig("my_figure.jpg", dpi=300, bbox_inches="tight")
plt.show()