# Introduction to Matplotlib

### Contents

### Data visualization

Data visualization is the representation of data in a graphical format. It helps putting data in a visual form. Information, relationships, patterns that might go unnoticed in a text-based format can be easily recognized with data visualization software. This is because the human brain can understand and process visuals such as images, graphs or charts more easily compared to having the data in spreadsheets or in the form of reports. Data visualizations can turn large and small datasets into visuals.

The table below displays an independent variable ‘x’ and three functions a,b and c. Each of these functions are dependent on the variable ‘x’. Let us use the data in the table below to plot these functions.

From the plot, we can observe the relation between the variable and each of the functions and can infer that the cubic function(c) grows much faster compared to the identity function(a) and the square function(b).

In this example, we have a small dataset, so analysing data from the table is easy. But what if we have a dataset with millions of entries or a complex function to be analysed. In that case, having a graphical representation of data would be useful. There are various types of graphs or charts to represent data in a visual form. The type of data given and what we want to convey to the user determine the appropriate graph to be used. Line plots, Pie charts, Bar charts, Histogram, Scatter Plots etc., are few examples of graphs.

### Matplotlib

This course will take an in-depth look at the Matplotlib tool for visualization in Python.

Matplotlib is a Python package that is widely used throughout the scientific Python community to create high-quality and publication-ready graphics.

Matplotlib is Python’s alternative to MATLAB and it has the advantage of being free and open-source, whereas MATLAB is expensive and closed source.

The Matplotlib library provides the pyplot module, which contains functions which closely resemble the MATLAB plotting syntax and functionality.

Matplotlib is built on Numpy arrays. It supports a wide range of export formats suitable for both web and print publishing. It supports high-quality output formats such as PNG, PDF, SVG, EPS and PGF.

#### Installing Matplotlib

#### Install Matplotlib with pip

Matplotlib can be installed using the Python package manager, pip. To install Matplotlib with pip, open a terminal window and type:

$ pip install matplotlib

This command installs Matplotlib in the current working Python environment.

#### Install Matplotlib with the Anaconda distribution of Python

The easiest way to install Matplotlib is to download and install the Anaconda distribution of Python. The Anaconda distribution of Python comes with Matplotlib included and no further installation steps are required. You can download the latest version of Anaconda by following this link – https://www.anaconda.com/download/.

#### Backend

Matplotlib uses a backend to render the plots. Backend is a utility used to create graphs. There are two types of backends, interactive and non-interactive. Interactive backends display the figure in a graphical user interface, which allows us to pan and zoom the figure. Non-interactive backends are used to produce image files. Matplotlib supports the following backends:

**Backends**: GTKAgg, GTK3Agg, GTK, GTKCairo, GTK3Cairo, WXAgg

The Jupyter Notebook supports the ‘inline’ backend. With this backend, the output of plotting commands is displayed inline, that is directly below the code cell that produced it. The inline backend renders a static or a stand alone plot. The resulting plots will be stored in the notebook document.

The ‘inline’ backend can be invoked using the following command: %matplotlib inline

The Jupyter notebook also supports the ‘notebook’ backend which renders an interactive plot. Just below the plot, we can find a toolbar to switch views, pan, zoom and download options.

The ‘notebook’ backend can be invoked using the following command: %matplotlib notebook

### Basic Plotting with Matplotlib

```
%matplotlib inline
from matplotlib import pyplot as plt
plt.plot([1,2,3,4,5],[1,2,3,4,5])
plt.show()
```

```
%matplotlib notebook
from matplotlib import pyplot as plt
plt.plot([1,2,3,4,5],[5,4,3,2,1])
plt.show()
```