The Matbplotlib Pyplot Library

Try me

Open In ColabBinder

The Matplotlib (Mathematical plotting Library) is a Python charting package that allows us to render rich interactive graphics from Python data structures, including Numpy arrays.

To use Matplotlib in your Notebooks and programs, you first need to import the Pyplot package (in this example we use the alias plt):

[1]:
import matplotlib.pyplot as plt

Matplotlib modes

We can use two different modes in Notebooks:

  • Notebook: This will create interactive graphics where you can zoom-in and move around the graph as well as export. You can use this mode for experiments, to interact with data directly in the book, but it is not recommended to use it for presentation.

  • Inline: This will create a static graphic with the selected properties. We will work with this mode in this course

[2]:
#We set the mode inline of matplotlib to get the result at the output of the cell code
%matplotlib inline

Main Concepts

Matplotlib introduces the following concepts:

  • Figure: A figure is the canvas where we will plot our data. Note that a figure is not a graph, but the container that allows us to arrange one or more graphs for presentation.

  • Plots: Plots or axes are the actual graphs included in the figure. A graph has a title, a legend and 2 or more axis.

  • Axis: Conform the limits of the plot.

Creating bi-directional charts

In this course we will work mainly with bi-directional charts, therefore we will create figures with one axe (or plot) and two axis. There is a convenient Matplotlib function for this exact purpose:

  • plot([x],y,[fmt],[kwargs]): This function takes an optional parameter x, a parameter y and optional parameters to format the line and pass additional formatting parameters to matplotlib.

For instance,

plot(y)

will plot the data in y versus the corresponding indexes.

plot(x,y)

will plot y versus x

plot(x,y,'o--')

will plot y versus x using a dotted line and circular markers

plot(x,y,'o--', label='random data')

will plot y versus x using a dotted line and circular markers and create a label for y with the text “random data”. Note that for the label parameter we can use expressions, for instance to write equations. For instance, using:

`python label='$x_{1}=f(y,\phi)$'

will create a label displayed as \(x_{1}=f(y,\phi)\)

Note that the plot function will not render the title or the legend of the plot. We need to use the functions:

  • legend([kwargs]): will render the legend, we can provide optional parameters to format the legend.

  • title(label): will render the title with the text provided in the label parameter.

There are other signatures and many formatting parameters defined in the matplotlib library, but this is outside the scope of this tutorial. Check the documentation whenever you want to try to apply a different style.

[3]:
import numpy as np;
y = 200 + np.random.randn(100)
x = np.arange(y.size)

plt.plot(x,y,'o--', label='random data')
#Add legend
plt.legend()
plt.title('Random data')
[3]:
Text(0.5, 1.0, 'Random data')
../../_images/Data_Manipulation_tutorials_Matplotlib_tutorial_6_1.png

The plot function allows us to effectively chart different lines into the same plot, just adding as many calls to the function as we want in the same code cell. For instance, in the example below we add two lines by calling twice the plot function with different data.

[35]:
import numpy as np;
y = 200 + np.random.randn(100)
y2 = 100 + np.random.randn(100)
x = np.arange(y.size)

plt.plot(x,y,'o--', label='random data')
plt.plot(x,y2,'*-.', label='more random data')
#Add legend
plt.legend()
plt.title('Random data')
[35]:
Text(0.5, 1.0, 'Random data')
../../_images/Data_Manipulation_tutorials_Matplotlib_tutorial_8_1.png

Formatting the axes

There are many different ways to format the axis. We will use the following convenient functions:

plt.xlim((0, 20)) plt.ylim((0, 15)) plt.xlabel(r’\(x_{1}\)’) plt.ylabel(r’\(x_{2}\)’)

  • xlim((left, right)): sets the limits of the x axis to the values provided. The limits are provided as a tuple of integer values, first the limit to the left of the plot (lower value) and then the limit to the right (higher value).

  • ylim((bottom, top)): sets the limits of the y axis to the values provided. The limits are provided as a tuple of integer values, first the limit to the bottom (lower value) and then the limit to the top (higher value).

  • xlabel(label): sets the label for the x axis, using the text provided in the label parameter. We can provide expressions for instance to use equations in the labels (see example below).

  • ylabel(label): sets the label for the y axis, using the same parameter as the xlabel function describe above.

[10]:
import numpy as np;
y2 = 200 + 50*np.random.randn(100)
x = np.arange(y.size)

plt.plot(x,y2,label='random data')
#Add legend
plt.legend()
plt.title('Random data')

# sets limits
plt.xlim((0,100))
plt.xlabel(r'$x$')

plt.ylim((0,310))
plt.ylabel(r'$y=200 + 50·rand()$')

[10]:
Text(0, 0.5, '$y=200 + 50·rand()$')
../../_images/Data_Manipulation_tutorials_Matplotlib_tutorial_10_1.png

Filling areas

The function fill_between allows us to fill the area between any two lines:

fill_between(x, y, y2=0, where=None, kwargs): Fills the area between lines y (upper limit of the area) and y2 (bottom limit of the area), both versus x. Also, both y and y2 can either be a line or a numerical value. If a scalar value is used, a horizontal line crossing the y axis at the numerical value provided is used as the bottom limit of the area. The optional where parameter is used to provide an expression in x to limit the area in the horizontal axis. Additional parameters are used to format the area. For instance, in the example below, we set up the color of the area and its transparency.

[37]:
import numpy as np;
y_rand = 200 + 50*np.random.randn(100)

x = np.arange(y.size)
y_const = 50 + 0*x
plt.plot(x,y_rand,label='random data')
plt.plot(x,y_const, label='a straight line')
#Add legend
plt.legend()
plt.title('Random data')

# sets limits
plt.xlim((0,100))
plt.xlabel(r'$x$')

plt.ylim((0,310))
plt.ylabel(r'$y=200 + 50·rand()$')
plt.fill_between(x, y_rand, y2=y_const, where=x<60, color='green', alpha=0.5)
# since y2 is a constant, the line above is equivalente to
#plt.fill_between(x, y_rand, 50, where=x<60, color='green', alpha=0.5)
[37]:
<matplotlib.collections.PolyCollection at 0x1e8fc64ccc0>
../../_images/Data_Manipulation_tutorials_Matplotlib_tutorial_12_1.png

Note that in the last example, we are drawing the area between a line and a horizontal line and therefore we could just use y2=50 in the fill_between function call.

Drawing vertical lines

In order to draw a vertical line, we use a different pyplot function:

  • axvline(x=0, ymin=0, ymax=1,kwargs)**: Draws a horizontal line at the scalar position x of the horizontal axis, the additional ymin and ymax parameters allow to set up the lower and upper limits of the line. Additional parameters are used to format the line.

Similarly, the function axhline can be used to draw horizontal lines.

[43]:
import numpy as np;
y_rand = 200 + 50*np.random.randn(100)

x = np.arange(y.size)
plt.plot(x,y_rand,label='random data')

#Draw horizontal line
plt.axvline(60,0,310,color='black')

#Add legend
plt.legend()
plt.title('Random data')

# sets limits
plt.xlim((0,100))
plt.xlabel(r'$x$')

plt.ylim((0,310))
plt.ylabel(r'$y=200 + 50·rand()$')
plt.fill_between(x, y_rand, 0, where=x<=60, color='green', alpha=0.5)
# since y2 is a constant, the line above is equivalente to
#plt.fill_between(x, y_rand, 50, where=x<60, color='green', alpha=0.5)
[43]:
<matplotlib.collections.PolyCollection at 0x1e8fc7cecf8>
../../_images/Data_Manipulation_tutorials_Matplotlib_tutorial_15_1.png

Extra: Annotating a point of the graph

As an extra (not needed to complete assigments, but used in the slides), we can annotate a point of the graph with the function annotate:

  • **annotate(s, xy, *args, kwargs): Annotates the point xy with the string s

See the example below:

import numpy as np; y_rand = 200 + 50*np.random.randn(100) x = np.arange(y.size) #Calculate minimum coordinates ymin = np.min(y_rand) xmin=x[np.where(y_rand==ymin)] # Calculate maximum coordinates ymax = np.max(y_rand) xmax = x[np.where(y_rand == ymax)] plt.plot(x,y_rand,label='random data') #Annotate minimum plt.annotate('Min Value',(xmin[0],ymin)) #Annotate maximum plt.annotate('Max value', (xmax[0], ymax)) #Add legend plt.legend() plt.title('Random data') # sets limits plt.xlim((0,100)) plt.xlabel(r'$x$') plt.ylim((0,350)) plt.ylabel(r'$y=200 + 50·rand()$') plt.fill_between(x, y_rand, 0, color='green', alpha=0.5) # since y2 is a constant, the line above is equivalente to #plt.fill_between(x, y_rand, 50, where=x<60, color='green', alpha=0.5)
[ ]: