Introduction to matplotlib

Matplotlib Links to an external site.is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib Links to an external site.is probably the single most used Python package for 2D-graphics. It provides both a very quick way to visualize data from Python and publication-quality figures in many formats. There are a few other ways to plot in Python - Plotnine, seaborne, etc... that are even prettier and more Grammar for Graphicsy, but we will review Matplotlib here since its the simplest and most used.

Here is a first example. (This works directly in the browser! - Run it!!!) 

You first import matplotlib with the following command:

import matplotlib.pyplot as plt

And we see we are generating N random numbers in 2D (x,y) and random colors and areas and using a scatterplot to simply plot them and show the circles.

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

Here is the full example below:

Matplotlib comes with a set of default settings that allow customizing all kinds of properties. You can control the defaults of almost every property in matplotlib: figure size and dpi, line width, color and style, axes, axis and grid properties, text and font properties and so on. While matplotlib defaults are rather good in most cases, you may want to modify some properties for specific cases.

In this example we start to change some of the default graphing values: setting limits, tick values, show how to save an image to disk, etc...

References

  1. Matplotlib examples Links to an external site.
  2. Matplotlib Tutorial Links to an external site. - Nicolas Rougier