Lecture 4 : Data visualization with Python

April, 2022 - François HU

Master of Science - EPITA

This lecture is available here: https://curiousml.github.io/

image-4.png

Table of contents

Introduction to Matplotlib

  1. Pyplot interface
  2. Line plot
  3. Scatter plot
  4. Histogram
  5. Global customization
  6. Subplots
  1. Object-oriented interface
  2. [optional] Other plots: a quick overview

Exercices

Introduction to Matplotlib

Data visualization (or DataViz) is a process that allows you to understand data (e.g. patterns, trends or correlations) by representing it in a graphic form. For that purpose, there are many python packages available: in this notebook, we will visualize data thanks to Matplotlib which is the most used Python package for plotting.

Matplotlib is quite popular in Python thanks to its low-level coding which offers lots of freedom. Note that many "advanced" data visualization packages are built on top of Matplotlib. For instance:

1. Pyplot interface

Pyplot is a submodule of matplotlib where it contains a collection of functions enables you to create or modify figures. Pyplot is very good for creating basic graphs like line charts, bar charts, histograms and many more. All the pyplot commands make changes and modify the same figure so that the state (i.e., the figure) is preserved through various function calls (i.e., the methods that modify the figure).

As usual, one can install the package Matplotlib with the command

pip install matplotlib

in anaconda prompt. After installing the package matplotlib one can import it alongside with the submodule pyplot and then rename it plt (frequently used) with the command:

In a notebook, the code command %matplotlib configures the package that you will use to draw a figure. It performs a number of processes to prepare the display of the figure. It is recommended here to used it with the argument inline, which indicates that the package is integrated in Notebook. This directive must be included at the very beginning of your script, even before the package import directives.

2. Line plot

A line plot is a graph that uses lines to connect individual data points. A line plot displays quantitative values over a specified interval. This is particularly useful to visualize a (mathematical) function (e.g. sine, cosine, exponential or our own function). Let us plot a sine curve. For that purpose we will use one of the most popular function in matplotlib.pyplot: plot (see documentation for more information)

Toy example

At the end of the last line, we add ; in order to prevent returning additional output.

Simple customization

We can customize (non-exhaustive, see documentation for more details):

3. Scatter plot

Instead of curves, we want to create a scatter plot, a graph in which the values of two variables are plotted along two axes

Toy example

Let us first generate random points in the space $[0,1] \times [0,1]$

In Matplotlib we can use the scatter method for creating a scatter plot

Simple customization

The scatter plot can be customized. For instance we can customize (non-exhaustive, see documentation for more details):

4. Histogram

A histogram is a graphical display of numerical data by showing the number of data points that fall within a specified range of values (called "bins").

In Matplotlib we can create a Histogram using the hist method

Toy example

Simple customization

For histograms, we can customize (non-exhaustive, see documentation for more details):

We can also let the method hist to return a probability density instead of the raw count with the argument density=True

5. Global customization

More generally, every figure can be customized. In a nutshell one can customize (non exhaustive):

In pyplot, the methods for drawing a graph or editing a label apply by default to the last current state (last instance of a subplot or last instance of an axis for example). As a consequence, you must design your codes as a sequence of instructions (for example, you must not separate instructions that refer to the same graph in two different Notebook cells).

Remark: here, let us use the sine and cosine plot of the section 2 as a toy example.

Adding axes and title

Adding another line

Adding a legend

Customize the figure size