5 Best Ways to Make Several Plots on a Single Page Using Matplotlib in Python

πŸ’‘ Problem Formulation: When analyzing data, it’s often useful to visualize different aspects of the data simultaneously for comparison. For instance, a data scientist might want to plot temperature data against time, with separate graphs for different cities on the same page. This technique facilitates easier analysis and comparison of the data.

Method 1: Using subplots

Creating multiple plots on a single page in Python is straightforward using the matplotlib library’s subplot function. This method allows a user to arrange the plots in a grid with specified rows and columns, each containing a plot.

Here’s an example:

import matplotlib.pyplot as plt

# Creating a figure and a grid of subplots
fig, axs = plt.subplots(2, 2)

# Data for plotting
x = range(0, 100)
y1 = [value ** 2 for value in x]
y2 = [value ** 0.5 for value in x]
y3 = [value for value in x]
y4 = [value * 2 for value in x]

# Plotting data on the grid
axs[0, 0].plot(x, y1)
axs[0, 1].plot(x, y2)
axs[1, 0].plot(x, y3)
axs[1, 1].plot(x, y4)

plt.show()

Output: This code generates a window with a 2×2 grid of plots.

In this code, plt.subplots(2, 2) creates a 2×2 grid of axes. Data sets are then plotted on these axes individually. This approach is great for organizing related plots in a structured manner.

Method 2: Using subplot2grid

The subplot2grid method is a more flexible alternative to subplots, allowing for the creation of subplots in a grid with varying sizes. Subplots can span multiple rows or columns, giving the user control over the layout.

Here’s an example:

import matplotlib.pyplot as plt

# Creating subplots with varying sizes
ax1 = plt.subplot2grid((3,3), (0,0), colspan=2)
ax2 = plt.subplot2grid((3,3), (0,2), rowspan=3)
ax3 = plt.subplot2grid((3,3), (1,0), colspan=2, rowspan=2)

# Plot data
ax1.plot([1, 2, 3], [1, 2, 3])
ax2.plot([1, 2, 3], [3, 2, 1])
ax3.plot([3, 2, 1], [1, 2, 3])

plt.tight_layout()
plt.show()

Output: This code results in a grid with three plots of different sizes.

Using subplot2grid, one can specify the position and span of each subplot to customize the layout to their needs. It’s an excellent option for more complex arrangements.

Method 3: Using the GridSpec class

The GridSpec class from Matplotlib enables a more advanced subplot grid configuration. With GridSpec, you have complete control over the positioning of the plots, including the allocation of space between them.

Here’s an example:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# Create a GridSpec layout
fig = plt.figure()
gs = gridspec.GridSpec(2, 3)

ax1 = plt.subplot(gs[0, :2])
ax2 = plt.subplot(gs[0, 2])
ax3 = plt.subplot(gs[1, 0])
ax4 = plt.subplot(gs[1, 1:])
 
# Plot data
# ...

plt.tight_layout()
plt.show()

Output: This code creates four plots with custom positioning within a 2×3 grid.

The GridSpec class allows for sophisticated subplot layouts that can be adjusted to suit various analysis needs, offering more flexibility than subplots and subplot2grid.

Method 4: Using add_subplot

The add_subplot method is part of the figure class and allows for the dynamic addition of subplots to a figure. This is particularly useful when the number of subplots or their configuration is not known beforehand.

Here’s an example:

import matplotlib.pyplot as plt

# Creating a new figure
fig = plt.figure()

# Adding subplots dynamically
for i in range(1, 5):
    ax = fig.add_subplot(2, 2, i)
    ax.plot([1, 2, 3], [i, i*2, i*3])

plt.show()

Output: The code generates a figure with a 2×2 grid of incremental plots.

This method is useful when creating plots in a loop or when the number of plots can change dynamically in the program.

Bonus One-Liner Method 5: Using pyplot.subplots() in a single line

For those who prefer conciseness in code, plt.subplots() can be used in a single line to create multiple plots. The returned figure and array of axes can then be used to customize subplots quickly.

Here’s an example:

fig, axs = plt.subplots(2, 2); [ax.plot(range(10)) for row in axs for ax in row]; plt.show()

Output: A compact grid of plots with sequentially increasing lines.

This one-liner is best for quickly creating standard grids of plots without the need for complex layouts or customization.

Summary/Discussion

  • Method 1: Using subplots. Great for structured and grid-based layouts. It can become less convenient for highly customized arrangements.
  • Method 2: Using subplot2grid. Offers more flexibility than using subplots by allowing varying subplot sizes. Can be slightly more complex for newcomers.
  • Method 3: Using the GridSpec class. Maximizes layout control for complex subplot arrangements. Requires a more in-depth understanding of Matplotlib layout concepts.
  • Method 4: Using add_subplot. Allows dynamic subplot creation. It is ideal for cases where the number and configuration of subplots are variable.
  • Method 5: Bonus One-Liner. Perfect for rapid plotting without the need for specific configurations. Not suitable for situations demanding customized layouts.