Book Image

matplotlib Plotting Cookbook

By : Alexandre Devert
Book Image

matplotlib Plotting Cookbook

By: Alexandre Devert

Overview of this book

Table of Contents (15 chapters)
matplotlib Plotting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Compositing multiple figures


When examining some data, we might want to see several aspects of it at once. For instance, with population statistics data from one country, we would like to see the male/female age pyramid, the wealth repartition, and the population size per year as three distinct graphics. matplotlib offers the possibility to composite several figures together. Since Version 1.2, the API for this is really convenient. In this recipe, we are going to see how to compose several figures together.

How to do it...

We are going to use the pyplot.subplot2grid() function as follows:

import numpy as np
from matplotlib import pyplot as plt

T = np.linspace(-np.pi, np.pi, 1024)

grid_size = (4, 2)

plt.subplot2grid(grid_size, (0, 0), rowspan = 3, colspan = 1)
plt.plot(np.sin(2 * T), np.cos(0.5 * T), c = 'k')

plt.subplot2grid(grid_size, (0, 1), rowspan = 3, colspan = 1)
plt.plot(np.cos(3 * T), np.sin(T), c = 'k')

plt.subplot2grid(grid_size, (3, 0), rowspan=1, colspan=3)

plt.plot(np.cos...