5 Best Ways to Use the Subplot Function to Create Two Graphs in Matplotlib Python

Rate this post

πŸ’‘ Problem Formulation: When working with data visualization in Python, you might encounter the need to display multiple graphs within a single figure for a comparative or complementary visual analysis. For example, you might want to compare the trends of two different datasets side-by-side. The subplot function in Matplotlib is a tool designed to create such multiple plots. This article demonstrates how to use the subplot function to effectively create two separate graphs within a single figure.

Method 1: Using subplot() Directly

Create multiple axes in a single figure manually by calling subplot() with specific grid parameters. This grid is defined by the number of rows and columns, as well as the plot index for positioning.

Here’s an example:

import matplotlib.pyplot as plt

# Create the first subplot
plt.subplot(1, 2, 1)
plt.plot([1, 2, 3], [4, 5, 6])

# Create the second subplot
plt.subplot(1, 2, 2)
plt.plot([1, 2, 3], [6, 5, 4])

plt.show()

The code will create a figure with two side-by-side plots.

This code snippet creates a figure with two horizontal plots. The first call to subplot(1, 2, 1) sets up an area for the first graph in a 1-row by 2-column grid. The second call to subplot(1, 2, 2) creates the space for the second graph. Finally, plt.show() displays the figure with both plots.

Method 2: Using subplots() Function

The subplots() function creates a full grid of subplots in a single step and returns a figure and an array of axes objects.

Here’s an example:

import matplotlib.pyplot as plt

# Create a grid of subplots
fig, axs = plt.subplots(1, 2)

# Plot on each subplot
axs[0].plot([1, 2, 3], [4, 5, 6])
axs[1].plot([1, 2, 3], [6, 5, 4])

plt.show()

This code results in a figure with two horizontally aligned plots.

This method leverages the subplots() function, offering a simpler way to generate a grid and access individual subplots through an array. The array axs provides axes objects for each subplot, allowing you to modify each independently before displaying them with plt.show().

Method 3: Using Object-Oriented Interface

This method uses Matplotlib’s object-oriented API, where you first create a figure object and then add subplots to it.

Here’s an example:

import matplotlib.pyplot as plt

# Create a figure object
fig = plt.figure()

# Add first subplot
ax1 = fig.add_subplot(121)
ax1.plot([1, 2, 3], [4, 5, 6])

# Add second subplot
ax2 = fig.add_subplot(122)
ax2.plot([1, 2, 3], [6, 5, 4])

plt.show()

The output will be two side-by-side graphs within the same figure.

The object-oriented approach offers finer control and customization. Each subplot is added using the add_subplot() method on the figure object with the grid parameters. Separate axes objects (ax1, ax2) are manipulated before the figure is displayed using plt.show().

Method 4: Adjusting Subplot Layout

Customize the spacing between subplots using the subplots_adjust() method, providing parameters for padding and space between graphs.

Here’s an example:

import matplotlib.pyplot as plt

# Create subplots
fig, axs = plt.subplots(1, 2)

# Plot data
axs[0].plot([1, 2, 3], [4, 5, 6])
axs[1].plot([1, 2, 3], [6, 5, 4])

# Adjust the spacing
plt.subplots_adjust(wspace=0.5)

plt.show()

This will output two graphs with adjustable spacing in between.

Here, subplots_adjust() is utilized to increase the space between the two subplots, with wspace determining the width of the padding. This allows for a more readable layout, especially important when graphs have labels that might overlap.

Bonus One-Liner Method 5: Using pyplot shortcuts

When simplicity is key, use the concise one-liner plt.subplots() shortcut to define two plots.

Here’s an example:

import matplotlib.pyplot as plt

# Create subplots and plot in one line
fig, (ax1, ax2) = plt.subplots(1, 2); ax1.plot([1, 2, 3], [4, 5, 6]); ax2.plot([1, 2, 3], [6, 5, 4])

plt.show()

The snippet above quickly produces two side-by-side graphs.

This one-liner combines the subplot creation and plotting steps into one compact statement. It’s a quick and dirty way to create multiple graphs when customization is not a priority and is perfect for fast prototyping.

Summary/Discussion

  • Method 1: Using subplot() directly. Pros: Explicit control over subplot positioning. Cons: Requires manual setup and can become verbose for complex layouts.
  • Method 2: Using subplots() function. Pros: Quick and easy setup. Cons: Less flexibility for irregular grid layouts.
  • Method 3: Object-Oriented Interface. Pros: High level of customization. Cons: Slightly more complex syntax.
  • Method 4: Adjusting Subplot Layout. Pros: Custom spacing between plots. Cons: Requires knowledge of subplots_adjust() parameters.
  • Bonus Method 5: pyplot one-liner. Pros: Maximum conciseness. Cons: Limited customization and potential readability issues for complex code.