Skip to main content Accessibility help
×
Hostname: page-component-77c89778f8-sh8wx Total loading time: 0 Render date: 2024-07-22T06:27:05.126Z Has data issue: false hasContentIssue false

7 - Matplotlib

Published online by Cambridge University Press:  05 February 2016

Christian Hill
Affiliation:
University College London
Get access

Summary

Matplotlib is probably the most popular Python package for plotting data. It can be used through the procedural interface pylab in very quick scripts to produce simple visualizations of data (see Chapter 3) but, as described in this chapter, with care it can also produce high-quality figures for journal articles, books and other publications. Although there is some limited functionality for producing three-dimensional plots (see Section 7.2.3), it is primarily a two-dimensional plotting library.

Matplotlib basics

Matplotlib is a large package organized in a hierarchy: at the highest level is the matplotlib.pyplot module. This provides a “state-machine environment” with a similar interface to MATLAB and allows the user to add plot elements (data points, lines, annotations, etc.) through simple function calls. This is the interface used by pylab, which was introduced in Chapter 3.

At a lower level, which allows more advanced and customizable use, Matplotlib has an object-oriented interface that allows one to create a figure object to which one or more axes objects are attached. Most plotting, annotation and customization then occurs through these axes objects. This is this approach we adopt in this chapter.

To use Matplotlib in this way, we use the following recommended imports:

import matplotlib.pyplot as plt

import numpy as np

Basic figures

Plotting on a single axes object

The top-level object, containing all the elements of a plot is called Figure. To create a figure object, call plt.figure. No arguments are necessary, but optional customization can be specified by setting the values described in Table 7.1. For example,

In [x]: # a default figure, with title “Figure 1”

In [x]: fig = plt.figure()

In [x]: # a small figure with red background

In [x]: fig = plt.figure(’Population density’, figsize=(4.5, 2.),

….: facecolor=’red’)

To actually plot data, we need to create an Axes object – a region of the figure containing the axes, tick-marks, labels, plot lines and markers, and so on. The simplest figure, consisting of a single Axes object, is created and returned with

In [x]: ax = fig.add_subplot(111)

The argument 111 here is a commonly used abbreviation for the tuple (1,1,1) specifying subplot 1 of a figure with 1 row and 1 column of subplots (see Section 7.1.3). The Axes object, ax, is the one on which we can actually plot the data with ax.plot. The essential features of this plot method were described in Chapter 3.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2016

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Matplotlib
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.007
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Matplotlib
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.007
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Matplotlib
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.007
Available formats
×