A document from MCS 275 Spring 2021, instructor Emily Dumas. You can also get the notebook file.

Quick intro to matplotlib

MCS 275 Spring 2021 - Emily Dumas

This is a quick tour of basic plotting with matplotlib. For more detail see:

You can install matplotlib on your own machine (e.g. python3 -m pip install matplotlib) but this may be unnecessary: Matplotlib is often most useful in a notebook setting, and it is already available on Google Colab.

Import matplotlib

In [2]:
# This submodule is all you need in most cases, and plt is a common
# abbreviated name to use for it.
import matplotlib.pyplot as plt

# For some things you need access to the parent module
# This also lets us check the installed version.
import matplotlib as mpl
mpl.__version__
Out[2]:
'3.1.2'
In [3]:
import numpy as np
In [3]:
#plt.style.available
In [4]:
#plt.style.use("seaborn-whitegrid")

First line plot

In [5]:
x = np.linspace(start=-3,stop=3,num=100)
y = np.exp(-x*x)
plt.plot(x,y)
plt.show()

Multiple plots and styling

In [11]:
x = np.linspace(start=-3,stop=3,num=100)
y = np.exp(-x*x)
y2 = 1.5*np.exp(-3*(x-2)**2)
y3 = 0.8*np.exp(-4*(x+1)**2)

x2 = np.linspace(start=-2,stop=4,num=150)
y4 = 0.5*np.exp(-(x2-2)**2)*np.sin(6*x2)

plt.plot(x,y)
plt.plot(x,y2)
plt.plot(x,y3)

plt.plot(x2,y4)

plt.show()

Adjusting axes

In [19]:
x = np.linspace(start=-3,stop=3,num=100)
y = np.exp(-x*x)
y2 = 1.5*np.exp(-3*(x-2)**2)
y3 = 0.8*np.exp(-4*(x+1)**2)

x2 = np.linspace(start=-2,stop=4,num=150)
y4 = 0.5*np.exp(-(x2-2)**2)*np.sin(6*x2)

plt.plot(x,y)
plt.plot(x,y2)
plt.plot(x,y3)

plt.plot(x2,y4)

plt.xlim(-1,3)
plt.ylim(0,1.6)

plt.show()

Parametric plot

In [23]:
t = np.linspace(0,2*np.pi,300)
x = np.cos(t)
y = np.sin(t)
plt.plot(x,y)
plt.plot(np.cos(3*t),np.sin(5*t))
plt.axis("equal")
Out[23]:
(-1.099942042095258,
 1.0999972400997742,
 -1.0999848204440221,
 1.0999848204440221)

Line plots can lie

In [24]:
x = np.linspace(start=-3,stop=3,num=100)
y = np.tan(x)
plt.plot(x,y)
Out[24]:
[<matplotlib.lines.Line2D at 0x7f70bb100490>]

Adding a legend

In [20]:
x = np.linspace(start=-3,stop=3,num=100)
y = np.exp(-x*x)
y2 = 1.5*np.exp(-3*(x-2)**2)
y3 = 0.8*np.exp(-4*(x+1)**2)
plt.figure(figsize=(8,6))
plt.plot(x,y,label="$e^{-x^2}$")
plt.plot(x,y2,label="$1.5e^{-3(x-2)^2}$")
plt.plot(x,y3,label="$0.8e^{-4(x+1)^2}$")
plt.legend()
plt.show()
plt.savefig("three_gaussians.png",dpi=300)
plt.savefig("three_gaussians.pdf")
<Figure size 432x288 with 0 Axes>
In [26]:
x = np.linspace(start=-3,stop=3,num=100)
y = np.exp(-x*x)
y2 = 1.5*np.exp(-3*(x-2)**2)
y3 = 0.8*np.exp(-4*(x+1)**2)
plt.figure(figsize=(8,6))
plt.plot(x,y,label="$e^{-x^2}$",color="orange",linestyle="dashed",linewidth=5)
plt.plot(x,y2,label="$1.5e^{-3(x-2)^2}$",color="#FF0080")
plt.plot(x,y3,label="$0.8e^{-4(x+1)^2}$",linestyle="dotted")
plt.legend()
plt.show()
In [28]:
x = np.linspace(start=-3,stop=3,num=100)
y = np.exp(-x*x)
y2 = 1.5*np.exp(-3*(x-2)**2)
y3 = 0.8*np.exp(-4*(x+1)**2)
plt.figure(figsize=(8,6))
plt.plot(x,y,label="$e^{-x^2}$",color="orange",linestyle="dashed",linewidth=5)
plt.plot(x,y2,label="$1.5e^{-3(x-2)^2}$",color="#FF0080",marker="*")
plt.plot(x,y3,label="$0.8e^{-4(x+1)^2}$",linestyle="dotted")
plt.legend()
plt.show()

Scatter plots

In [34]:
# Fundamentally, plt.plot shows the same marker symbol
# (same size, shape, color) at each data point
n = np.array([1,1.5,2,2.5,3.5,5])
t = np.array([1.8,2.6,3.5,4.9,8.8,8.2])
plt.plot(n,t,marker="o",linestyle="")
Out[34]:
[<matplotlib.lines.Line2D at 0x7f70bae3f940>]
In [47]:
n = np.array([1,1.5,2,2.5,3.5,5])
t = np.array([1.8,2.6,3.5,4.9,8.8,8.2])
s = np.array([0.1,0.1,0.1,0.2,0.2,0.5])
c = np.array([1,2,3,5,8,20])
plt.scatter(n,t,s=250*s,c=c,marker="o",cmap="hsv")
plt.colorbar()
Out[47]:
<matplotlib.colorbar.Colorbar at 0x7f70b8a6e7f0>

Contour and density plots

In [4]:
x = np.linspace(-3,3,100)
y = np.linspace(-2,2,80)
xx,yy = np.meshgrid(x,y)
In [5]:
# f(x,y) = x**3 - 8x + 3*y**2
zz = xx**3 - 8*xx + 3*yy**2  # 80x100 matrix of values of f on the grid
In [6]:
# f(x,y) = 0.2?
plt.figure(figsize=(8,6))
plt.contour(xx,yy,zz,[0.2])
Out[6]:
<matplotlib.contour.QuadContourSet at 0x7fed1dbc8610>
In [7]:
plt.figure(figsize=(8,6))
plt.contour(xx,yy,zz)
plt.colorbar()
Out[7]:
<matplotlib.colorbar.Colorbar at 0x7fecd65aa310>

Adding labels to contours

plt.clabel adds labels to an existing contour plot. Its argument is the return value of a previous call to plt.contour.

In [8]:
plt.figure(figsize=(8,6))
contours = plt.contour(xx,yy,zz,15,cmap="magma")
plt.title("Contour plot")
plt.clabel(contours) # add inline labels to the contours
plt.colorbar()
Out[8]:
<matplotlib.colorbar.Colorbar at 0x7fecd2cc4b20>

Density plots with plt.imshow

In [11]:
plt.figure(figsize=(8,6))
plt.imshow(zz,extent=[np.min(x),np.max(x),np.min(y),np.max(y)],origin="lower")
# origin="lower" means the first row of zz appears at the bottom of the plot.
# That's correct since our meshgrid has smallest y values in the first row.
plt.title("Density plot")
plt.colorbar()
Out[11]:
<matplotlib.colorbar.Colorbar at 0x7fecd2318790>
In [12]:
plt.figure(figsize=(8,6))
contours = plt.contour(xx,yy,zz,15,colors="white")
plt.title("Contour and density plot")
plt.clabel(contours) # add inline labels to the contours
plt.imshow(zz,extent=[np.min(x),np.max(x),np.min(y),np.max(y)])
plt.colorbar()
Out[12]:
<matplotlib.colorbar.Colorbar at 0x7fecd22195b0>