5 Best Ways to Show an Axes Subplot in Python

πŸ’‘ Problem Formulation: When visualizing data in Python, we often need to display multiple charts within a single figure to compare different datasets or aspects of data. Creating subplots is a common solution. The input in this case could be a set of data points and the desired output is a grid of charts, each representing a subset of the data with its own axes for better clarity and analysis.

Method 1: Using matplotlib.pyplot.subplot

Matplotlib’s subplot function is a straightforward way to create axes in a figure. It allows for the flexible designing of a grid of subplots within which you can place individual plots. The function requires the number of rows and columns in the grid and the index of the current plot.

Here’s an example:

import matplotlib.pyplot as plt

# Define the subplot grid and select the first subplot
plt.subplot(1, 2, 1)
plt.plot([0, 1, 2], [0, 1, 4])

# Select the second subplot in the grid
plt.subplot(1, 2, 2)
plt.plot([0, 1, 2], [0, 1, 0.5])

# Display the figure with subplots
plt.show()

The output is a figure with two subplots side by side.

This code snippet first divides the figure into a 1×2 grid (1 row and 2 columns) and creates the first subplot by selecting it with the index 1. A quadratic plot is drawn in the first subplot. The process is repeated for the second subplot but with a different dataset, generating a figure with two charts lined up horizontally.

Method 2: Using matplotlib.pyplot.subplots()

The subplots() function from Matplotlib simplifies the management of the figure and axes objects by creating them in one go. It returns a figure and an array of axes objects, which can be indexed to plot individual charts.

Here’s an example:

import matplotlib.pyplot as plt

# Create a 2x2 grid of axes
fig, ax_arr = plt.subplots(2, 2)

# Plotting on the top-left subplot
ax_arr[0, 0].plot([0, 1, 2], [0, 1, 4])

# Plotting on the bottom-right subplot
ax_arr[1, 1].plot([0, 1, 2], [0, 1, 0.5])

# Show the figure
plt.show()

The output is a 2×2 grid of charts, with plots in the top-left and bottom-right positions.

This block of code defines a 2×2 grid of subplots and receives a figure and a 2D-array of axes objects. It then plots data on the top-left and bottom-right axes. Empty subplots remain as placeholders for potential future data visualization.

Method 3: Using Figure.add_subplot()

This method involves creating an empty figure with Matplotlib’s Figure class and then adding subplots one by one with add_subplot. This method provides fine-grained control over the addition of subplots.

Here’s an example:

import matplotlib.pyplot as plt

# Create an empty figure
fig = plt.figure()

# Adding the first subplot
ax1 = fig.add_subplot(221) # Equivalent to 2x2 grid, 1st subplot
ax1.plot([1, 2, 3], [1, 2, 3])

# Adding the second subplot
ax2 = fig.add_subplot(222) # Equivalent to 2x2 grid, 2nd subplot
ax2.plot([1, 2, 3], [3, 2, 1])

# Display the figure
plt.show()

The output displays a 2×2 grid with subplots in the first two positions.

After instantiating a new figure, we add subplots by calling add_subplot() and specifying the grid size and subplot position. Two lines are plotted within the first two grid positions, showcasing how to individually control the layout of subplots.

Method 4: Using axes() for Custom Position

If you want precise control over the position of each subplot, the axes() function allows for specifying the exact coordinates of the subplot within the figure.

Here’s an example:

import matplotlib.pyplot as plt

# First custom subplot
ax1 = plt.axes([0.1, 0.1, 0.8, 0.4]) # left, bottom, width, height
ax1.plot([1, 2, 3], [1, 2, 3])

# Second custom subplot below the first
ax2 = plt.axes([0.1, 0.6, 0.8, 0.4])
ax2.plot([1, 2, 3], [3, 2, 1])

plt.show()

The output shows two custom-positioned subplots, one above the other.

With the axes() function, we explicitly define the position and size of each subplot as fractions of the figure’s size. The flexibility of this approach enables complex and nuanced subplot layouts.

Bonus One-Liner Method 5: Using pyplot.subplots_adjust()

Improving the visual presentation of subplots can be done quickly with the subplots_adjust() function, which allows for adjustment of the spacing between the subplots with a simple one-liner.

Here’s an example:

import matplotlib.pyplot as plt

# Create a 2x1 subplot grid
fig, axs = plt.subplots(2)

# Adjust spacing
plt.subplots_adjust(hspace=0.5)

# Add plots to each axis
axs[0].plot([0, 1, 2], [0, 1, 4])
axs[1].plot([0, 1, 2], [0, 2, 3])

plt.show()

The output shows two subplots with adjusted horizontal spacing between them.

This concise snippet uses subplots_adjust() after creating two vertical subplots to increase the horizontal space (hspace) between them. The method enhances readability without requiring a complex restructuring of the subplot layout.

Summary/Discussion

  • Method 1: subplot. Efficient for quick grid layouts. Limited by manual indexing.
  • Method 2: subplots(). Simplifies management with an array of axes. Less control over individual subplot positions.
  • Method 3: add_subplot() to a Figure object. Good for programmatic subplot insertion. Slightly more verbose.
  • Method 4: axes() for custom positions. Offers precise control. Can be complex for multiple subplots.
  • Method 5: One-liner, subplots_adjust(). Perfect for fine-tuning spacing. Only affects subplot layout, not creation.