Course contentsShow
← Data Science
Lesson 1275 of 2,14532. Visualization with Matplotlib and SeabornFree lesson

Indexing and Accessing Individual Axes

Master indexing techniques to access and modify individual subplot axes in 1D and 2D arrays.

Indexing and Accessing Individual Axes

You'll learn how to precisely target and customize individual subplots in a multi-panel figure by understanding how Matplotlib organizes subplot axes.

Understanding the Axes Array

When you create multiple subplots using plt.subplots(), Matplotlib returns two things: the overall figure and an array of axes objects. Think of this array like a spreadsheet—you need to know the "address" of each cell to modify it.

For a 1D array (single row or column of subplots), access is straightforward using a single index:

fig, axes = plt.subplots(1, 3)  # 1 row, 3 columns
axes[0].plot([1, 2, 3], [1, 4, 9])  # Left subplot
axes[1].plot([1, 2, 3], [2, 3, 4])  # Middle subplot
axes[2].plot([1, 2, 3], [5, 2, 6])  # Right subplot

For a 2D array (grid of subplots), use two indices—row first, then column:

fig, axes = plt.subplots(2, 2)  # 2x2 grid
axes[0, 0].plot([1, 2], [1, 2])  # Top-left
axes[0, 1].bar([1, 2], [3, 4])   # Top-right
axes[1, 0].scatter([1, 2], [2, 1])  # Bottom-left
axes[1, 1].hist([1, 2, 2, 3, 3, 3])  # Bottom-right

Why This Matters

Each axes object can be customized independently—different plot types, labels, colors, and scales. By indexing correctly, you control exactly which subplot you're modifying, enabling precise multi-panel visualizations where each panel tells part of your data story.

Key Takeaway: Use single indexing axes[i] for 1D subplot arrays and double indexing axes[row, col] for 2D grids to access and customize individual subplots independently.