This notebook is (will be) as small crash course on the functionality of the Matplotlib Python module for creating graphs (and embedding it in notebooks). It is of course no substirute for the proper Matplotlib thorough documentation.
First we need to import the library in our notebook. There are a number of different ways to do it, depending on what part of matplotlib we want to import, and how should it be imported into the namespace. This is one of the most common ones; it means that we will use the plt. prefix to refer to the Matplotlib API
In [ ]:
import matplotlib.pyplot as plt
We also need to add a bit of IPython magic to tell the notebook backend that we want to display all graphs within the notebook (otherwise they would generate objects instead of displaying into the interface; objects that we later can output to file or display explicitly).
This is done by the following declaration
In [ ]:
%matplotlib inline
Matplotlib allows extensive customization of graph aspect. Some of these customizations come together in "styles". Let's see which styles are available:
In [ ]:
print plt.style.available
In [ ]:
# Let's choose one style. And while we are at it, define thicker lines and big graphic sizes
plt.style.use('bmh')
plt.rcParams['lines.linewidth'] = 1.5
plt.rcParams['figure.figsize'] = (15, 5)
Without much more ado, let's display a simple graphic. For that we define a vector variable, and a function of that vector to be plotted
In [ ]:
import numpy as np
x = np.arange( -10, 11 )
y = x*x
And we plot it
In [ ]:
plt.plot(x,y)
plt.xlabel('x');
plt.ylabel('x square');
We can extensively alter the aspect of the plot. For instance, we can add markers and change color:
In [ ]:
plt.plot(x,y,'ro-');
Matplotlib command has two variants:
(example of object oriented syntax pending)
In [ ]:
%matplotlib notebook
In [ ]:
# import matplotlib
# matplotlib.use('nbagg')