UP | HOME

Matplotlib

Table of Contents

Press m to reveal the entire page or ? to show all shortcuts.

Matplotlib is the standard plotting library for producing publication-quality graphics in Python. It began life as an imitation of the plotting infrastructure of Matlab, and has evolved a bit towards more object orientation. As far as I can tell from the documentation, the object-oriented approach is recommended, especially for folks just beginning.

Before getting into the details, I feel it incumbent to point out that an alternative (rival?) environment has arisen in the last several years and it has a deep connection to HMC: it was cowritten by a brilliant former thesis student of mine, Alex Johnson (’99) (see the Plotly about page for details.) It’s called plotly, offers serious competition to Matplotlib, and appears to be going after the big data community in a serious way. Besides Matplotlib, we may use plotly and/or dash, as well.

1. Using Matplotlib in a Jupyter notebook

Although Matplotlib can be used either from the Python command line or in a straight Python program, the primary way we will use Matplotlib is within a Jupyter notebook. For this purpose, we will find it very helpful to include the following line at the top of the notebook:

%matplotlib notebook

This bit of “magic” tells Jupyter to take the output from a plotting command and display it in the output of the cell which generated it, rather than opening a graphics window and pausing until that window is dismissed (which is the behavior in a straight Python environment).

Author: Peter N. Saeta

Created: 2022-09-16 Fri 21:18

Emacs 25.3.50.1 (Org mode 9.5.2)

Validate