Plotting

In Python, the defacto plotting library is called matplotlib. If you're using Anaconda to run Python, it's probably already installed.

matplotlib is very powerful, but using it can be very confusing, since it provides a rather complex interface. "With great power comes great responsibility", or something like that.

What one usually does is looks at the official gallery for an example that's something like what you want to plot.


In [1]:
import IPython
website = "http://matplotlib.org/gallery.html"
IPython.lib.display.IFrame(website, width=800, height=800)


Out[1]:

First, we'll work through the example presented on http://matplotlib.org/examples/statistics/errorbar_demo.html.

To use matplotlib, first we import it. Failing #1 of matplotlib is that you don't do this:


In [2]:
# import matplotlib

Instead, we do this:


In [3]:
import matplotlib.pyplot as plt

We use matplotlib through an interface called pyplot; the top-level import (just matplotlib) is used to access low-level settings and more advanced functionality.

By convention, and to shorten calling its functions, we import it as plt.

First we want to make some data. We're going to use NumPy to create our $x$ and $f(x)$.


In [4]:
import numpy as np

In [5]:
x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)

In [6]:
print(x)
print(y)


[ 0.1  0.6  1.1  1.6  2.1  2.6  3.1  3.6]
[ 0.90483742  0.54881164  0.33287108  0.20189652  0.12245643  0.07427358
  0.0450492   0.02732372]

Let's plot them!


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


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

Uh oh, why didn't that work?

This is failing #2 of matplotlib, in my opinion; it isn't immediately obvious how to show your plot! The reason is because we might want to add other things to our plot, like axis labels, a legend, some overlaid text, etc.

To show a plot (called a figure) in an IPython notebook, we need to execute an IPython "magic function":


In [8]:
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('pdf', 'svg')

Now when we call plt.show(), it will appear as a cell, and as a high-quality vector image.

Note that if you call plt.show() in a notebook without the % magic function, you'll crash the Python process.


In [9]:
plt.show()

Nothing happened...maybe call plt.plot() again?


In [10]:
plt.plot(x, y)


Out[10]:
[<matplotlib.lines.Line2D at 0x7f3c44034e80>]

We can add circular markers by setting an option in our plot call:


In [11]:
plt.plot(x, y, marker='o')


Out[11]:
[<matplotlib.lines.Line2D at 0x7f3c3b21dcf8>]

To add error bars, we change our plot command from plt.plot() to plt.errorbar():


In [12]:
plt.errorbar(x, y, xerr=0.2, yerr=0.4)


Out[12]:
<Container object of 3 artists>

We've sucessfully replicated the plot from the Matplotlib gallery example.

Sidenote: How do we get rid of that annoying text that's always showing up at the top of the output cell, right above the plot?

Supress it with a semicolon after the plot command:


In [13]:
plt.errorbar(x, y, xerr=0.2, yerr=0.4);



In [ ]: