This notebook contains an excerpt from the book Machine Learning for OpenCV by Michael Beyeler. The code is released under the MIT license, and is available on GitHub.

Note that this excerpt contains only the raw code - the book is rich with additional explanations and illustrations. If you find this content useful, please consider supporting the work by buying the book!

Visualizing Data Using Matplotlib

Knowing how to load data is of limited use if we don't know how to look at the data. Thankfully, there is Matplotlib!

Matplotlib is a multiplatform data visualization library built on NumPy arrays—see, I promised you NumPy would show up again. It was conceived by John Hunter in 2002, originally designed as a patch to IPython to enable interactive MATLAB-style plotting from the command line. In more recent years, newer and shinier tools have popped up to eventually replace Matplotlib (such as ggplot and ggvis in the R language), but Matplotlib remains essential as a well-tested, cross-platform graphics engine.

Importing matplotlib

You might be in luck again: if you followed the advice outlined in the previous chapter and installed the Python Anaconda stack, you already have Matplotlib installed and are ready to go. Otherwise, you might want to visit http://matplotlib.org for installation instructions.

Just as we used np shorthand for NumPy, we will use some standard shorthands for the Matplotlib imports:


In [1]:
import matplotlib as mpl

In [2]:
import matplotlib.pyplot as plt

The plt interface is what we will use most often, as we shall see throughout the book.

Producing a simple plot

Without further ado, let's create our first plot.

Let's say we want to produce a simple line plot of the sine function, sin(x). We want the function to be evaluated at all points on the x axis where $0 \leq x < 10$. We will use NumPy's linspace function to create a linear spacing on the x axis, from x values 0 to 10, and a total of 100 sampling points:


In [3]:
import numpy as np

In [4]:
x = np.linspace(0, 10, 100)

We can evaluate the sine function at all points x using NumPy's sin function, and visualize the result by calling plt's plot function:


In [5]:
plt.plot(x, np.sin(x))


Out[5]:
[<matplotlib.lines.Line2D at 0x7f5af7400748>]

Did you try it yourself? What happened? Did anything show up?

In order for the plot to show up, we need to add some IPython magic. There are two options:

  • %matplotlib notebook will lead to interactive plots
  • %matplotlib inline will lead to static images of your plots

Check page 37 in the book to find out how to plot from a .py script or from within IPython.

In this book, we will generally opt for the inline option:


In [6]:
%matplotlib inline

Now let's try this again:


In [7]:
plt.plot(x, np.sin(x))


Out[7]:
[<matplotlib.lines.Line2D at 0x7f5af706d748>]

If you want to save the figure for later, you have the option to do so directly from within the Jupyter Notebook:


In [8]:
plt.savefig('figures/02.03-sine.png')


<matplotlib.figure.Figure at 0x7f5af7136748>