In this Python Matplotlib tutorial series, you will learn how to create and improve a plot in Python using pyplot. Matplotlib is a 2D plotting library written for Python. It consists of pyplot (in the code often shortened by “plt”), which is an object oriented interface to the plotting library. Matplotlib is an initiative of John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team. Lets stop talking and start creating some beautiful plots using Matplotlib!

## Data Visualization

In this post, we will gradually build a data visualization of two simple functions: sine and cosine. First, the main concepts are explained and then the step-by-step tutorial is explained.

## Figures & Axes & Axis

The figure can be seen as the canvas, on which all drawing components are plotted. The figure consists of axes, which are subdivisions of the figure. Each of the axes consists of one or more axis (horizontal (x-axis), vertical (y-axis) or even depth (z-axis)). All of this is visualized in the following picture:

## First simple plots

We will now create two very, very simple plots. One containing (sine) and the other one containing (cosine). For the calculations, we use NumPy (shortened by “np”). Matplotlib is often used in combination with Numpy. The code should be self-explaining (if not, please mention it in a comment).

This results into the following:

## Combine axes into one plot

It is also possible to combine the current axes into one plot by using the following code:

This results into the following data visualization of both functions:

However, notice that this plot is quite simple. It is not even clear which line belongs to what function. In the next few sections, we will add gradual improvements to the plot.

## Add a Legend

The first improvement is to add a legend. With the legend, it becomes clear what line belongs to what function (either sine or cosine).

This results into the following:

Notice the loc=0 parameter. This sets the location of the legend. loc=0 automatically selects the best place for the legend. There is one clear downside of our current figure now. All the functions have the same color. The next improvement adds color to the plot.

## Adding some color

Adding color is one of the easiest steps! This results into the following code changes:

We now get the following plot:

## Adding Title and Axis labels

Now we will add the title to our plot and add the axis labels by using the following code changes:

This results into the following plot:

This already is a great plot. In the last step, we will use a different rendering engine (build on top of Matplotlib) so all our code is reused but our plot gets improved!

## Using a different Rendering Style

We will now use the seaborn library to improve the style of the plot. For this, we will use the following code:

This results into our final plot:

## Some Excellent Videos

In this section, I will highlight some videos I found excellent for learning the concepts of pyplot.

The following video series introduce the basic concepts:

The next few videos are on advanced topics:

## Conclusion (TL;DR)

It is quite easy to create plots using Matplotlib. This tutorial shows the steps to build a plot gradually. If you have any questions, please let me know in the comments.

[amazon_link asins=’1783987545,1849513260,1847197906,1788295269′ template=’ProductGrid’ store=’ATVPDKIKX0DER’ marketplace=’US’ link_id=’818ef2ec-c9fc-11e7-9831-a19bb5b651fd’]