Matplotlib: Quick Reference for Commonly used Features

Last updated:

WIP Alert This is a work in progress. Current information is correct but more content will probably be added in the future.

Default imports and definitions for all examples below:

%matplotlib inline 
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 400)
sin = np.sin(x ** 2)
cos = np.cos(x ** 2)

procedural interface (PyPlot)

axis = singular, axes = plural

PyPlot is a state-machine interface to matplotlib.

examples:

# a figure with a 2x2 grid of axes (4 in total)
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2, 2)

# plot some line on the first subplot
ax1.plot(x,sin)

# # plot some line on the third subplot
ax3.plot(x,cos)
# an empty figure with no axes
fig = plt.figure()
# GCA = get current axes
ax = plt.gca()
ax.plot([3.1, 2.2])

object-oriented interface

TODO

Pylab

Use of pylab is discouraged

PyLab is the name given to a convenience module that imports all of PyPlot and NumPy.

It was created to make things easier for people coming from MATLAB.

References

Dialogue & Discussion