Matplotlib is the most popular data visualization library in Python. It allows us to create figures and plots, and makes it very easy to produce static raster or vector files without the need for any GUIs.

This tutorial is intended to help you get up-and-running with Matplotlib quickly. We’ll go over how to create the most commonly used plots, and discuss when to use each one.

Installing Matplotlib

If you have Anaconda, you can simply install Matplotlib from your terminal or command prompt using:

conda install matplotlib

If you do not have Anaconda on your computer, install Matplotlib from your terminal using:

pip install matplotlib

Now that you have Matplotlib installed, let’s begin by understanding the anatomy of a plot.

Anatomy of a Plot

There are two key components in a Plot; namely, Figure and Axes.

The Figure is the top-level container that acts as the window or page on which everything is drawn. It can contain multiple independent figures, multiple Axes, a subtitle (which is a centered title for the figure), a legend, a color bar, etc.

The Axes is the area on which we plot our data and any labels/ticks associated with it. Each Axes has an X-Axis and a Y-Axis (like in the image above). Let’s go ahead to making plots.

Getting Started

We will begin by importing Matplotlib using:

import matplotlib.pyplot as plt

Now that we have Matplotlib imported in our workspace, we need to be able to display the plots as it’s being created. If you’re using the Jupyter notebook we can easily display plots using: %matplotlib inline . However, if you’re using Matplotlib from within a Python script, you have to add plt.show() method inside the file to be able display your plot.

We are now ready to begin creating our plots. We can do this using two different approaches.

Two Approaches for creating Plots

Functional Approach: Using the basic Matplotlib command, we can easily create a plot. Let’s plot an example using two Numpy arrays x and y :

If no plot was displayed or if you’re using Matplotlib from within a Python script, don’t forget to add plt.show() at the last line to display your plot.

Now that we have a plot, let’s go on to name the x-axis, y-axis, and add a title using .xlabel() , .ylabel() and .title() using:

Imagine we needed more than one plot on that canvas. Matplotlib allows us easily create multi-plots on the same figure using the .subplot() method. This .subplot() method takes in three parameters, namely:

nrows : the number of rows the Figure should have.

: the number of rows the should have. ncols : the number of columns the Figure should have.

: the number of columns the should have. plot_number : which refers to a specific plot in the Figure .

Using .subplot() we will create a two plots on the same canvas:

Notice how the two plots have different colors. This is because we need to be able to differentiate the plots. This is possible by simply setting the color attribute to ‘red’ and ‘green’ as you can see above.

2. Object oriented Interface: This is the best way to create plots. The idea here is to create Figure objects and call methods off it. Let’s create a blank Figure using the .figure() method.

Now we need to add a set of axes to it using the .add_axes() method. The add_axes() method takes in a list of four arguments (left, bottom, width, and height — which are the positions where the axes should be placed) ranging from 0 to 1. Here’s an example:

As you can see, we have a blank set of axes. Now let’s plot our x and y arrays on it:

We can further add x and y labels and a title to our plot same way we did in the Function approach, but there’s a slight difference here. Using .set_xlabel() , .set_ylabel() and .set_title() let us go ahead and add labels and a title to our plot:

Remember, we noted that a Figure can contain multiple figures. Let’s try to put in two sets of figures on one canvas:

Now let’s plot our x and y arrays on the axes we have created:

Quick Exercise: Now that we have our plot ready, see if you can set the title, the x and y labels for both axes.

Like we did in the functional approach, we can also create multiple plots in the object-oriented approach using the .subplots() method, and NOT .subplot() . The .subplots() method takes in nrows , which is the number of rows the Figure should have, and ncols , the number of columns the Figure should have.

For example, we can create a 3 by 3 subplots like this:

What we have just done is that we used tuple unpacking to grab the axes from the Figure object which gave us a 3 by 3 subplots. As we see, there is an issue of overlapping in the subplots we created. We can deal with that by using .tight_layout() method to space it out:

The only difference between plt.figure() and plt.subplots() is that plt.subplots() automatically does what the .add_axes() method of .figure() will do for you based off the number of rows and columns you specify.

Now that we know how to create subplots, let’s see how we can plot our x and y arrays on them. We want to plot x, y on the axes at index position (0,1) and y, x on the axes at position (1,2) respectively:

Quick Exercise: Go ahead and see if you can set the title and the x and y labels for both axes.