Bird
Raised Fist0
Matplotlibdata~5 mins

Individual subplot customization in Matplotlib

Choose your learning style10 modes available
Introduction

Sometimes you want to change how each small plot looks inside a bigger picture. This helps show different details clearly.

You have multiple charts in one figure and want each to have its own title or labels.
You want to change colors or styles for each small plot separately.
You need to highlight or zoom in on one subplot without affecting others.
You want to add different grid lines or legends to each subplot.
You want to adjust axis limits or ticks individually for better clarity.
Syntax
Matplotlib
fig, axs = plt.subplots(nrows, ncols)
axs[row, col].plot(data)
axs[row, col].set_title('Title')
axs[row, col].set_xlabel('X label')
axs[row, col].set_ylabel('Y label')

axs is an array of subplot axes you can customize one by one.

Use axs[row, col] to select the subplot at that position.

Examples
Two plots side by side with different titles.
Matplotlib
fig, axs = plt.subplots(1, 2)
axs[0].plot([1, 2, 3])
axs[0].set_title('First plot')
axs[1].plot([3, 2, 1])
axs[1].set_title('Second plot')
Four plots in a grid, setting labels only on two of them.
Matplotlib
fig, axs = plt.subplots(2, 2)
axs[0, 0].set_xlabel('X1')
axs[1, 1].set_ylabel('Y2')
Two vertical plots with different line colors.
Matplotlib
fig, axs = plt.subplots(2, 1)
axs[0].plot([1, 4, 9], color='red')
axs[1].plot([9, 4, 1], color='blue')
Sample Program

This code creates four different plots in one figure. Each plot has its own style and labels. This shows how to change each subplot separately.

Matplotlib
import matplotlib.pyplot as plt

# Create 2x2 grid of subplots
fig, axs = plt.subplots(2, 2, figsize=(8, 6))

# Plot data and customize each subplot
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 0].set_title('Square Numbers')
axs[0, 0].set_xlabel('x')
axs[0, 0].set_ylabel('x squared')

axs[0, 1].plot([1, 2, 3], [1, 8, 27], 'r--')
axs[0, 1].set_title('Cube Numbers')
axs[0, 1].set_xlabel('x')
axs[0, 1].set_ylabel('x cubed')

axs[1, 0].bar(['A', 'B', 'C'], [5, 7, 3], color='green')
axs[1, 0].set_title('Bar Chart')

axs[1, 1].scatter([1, 2, 3], [3, 2, 1], color='purple')
axs[1, 1].set_title('Scatter Plot')

plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

Use plt.tight_layout() to avoid overlapping labels.

Remember to index subplots correctly: for 2D grids use axs[row, col], for 1D arrays use axs[index].

You can customize anything on each subplot like colors, titles, labels, grids, and more.

Summary

You can change each small plot inside a big figure separately.

Use the axes array to pick and customize each subplot.

This helps make your charts clearer and more informative.

Practice

(1/5)
1. What is the correct way to change the title of the second subplot when using fig, axs = plt.subplots(2, 2)?
easy
A. Use plt.title('New Title')
B. Use axs[1].set_title('New Title')
C. Use fig.set_title('New Title')
D. Use axs[0, 1].set_title('New Title')

Solution

  1. Step 1: Understand subplot indexing

    With plt.subplots(2, 2), axs is a 2x2 array. The second subplot in the first row is at position [0, 1].
  2. Step 2: Set title for specific subplot

    Use axs[0, 1].set_title('New Title') to change only that subplot's title.
  3. Final Answer:

    Use axs[0, 1].set_title('New Title') -> Option D
  4. Quick Check:

    Indexing subplots by row and column = A [OK]
Hint: Use axs[row, col] to target specific subplot [OK]
Common Mistakes:
  • Using axs[1] instead of axs[0, 1]
  • Setting title on fig instead of axs
  • Using plt.title affects whole figure, not subplot
2. Which of the following is the correct syntax to change the x-axis label of the first subplot in a 1D array of subplots fig, axs = plt.subplots(3)?
easy
A. axs[1].set_xlabel('X Label')
B. axs[0].set_xlabel('X Label')
C. axs.set_xlabel('X Label')
D. fig.set_xlabel('X Label')

Solution

  1. Step 1: Identify subplot indexing in 1D array

    With plt.subplots(3), axs is a 1D array with indices 0, 1, 2. The first subplot is at index 0.
  2. Step 2: Use set_xlabel on the correct subplot

    Use axs[0].set_xlabel('X Label') to set the x-axis label of the first subplot.
  3. Final Answer:

    axs[0].set_xlabel('X Label') -> Option B
  4. Quick Check:

    Index 0 for first subplot in 1D axs = D [OK]
Hint: Remember 1D axs uses zero-based indexing [OK]
Common Mistakes:
  • Using axs[1] for first subplot
  • Calling set_xlabel on fig or axs directly
  • Confusing 1D and 2D axs indexing
3. What will be the color of the line in the third subplot after running this code?
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
axs[1, 0].plot([1, 2, 3], [4, 5, 6], color='green')
plt.show()
medium
A. Blue
B. Red
C. Green
D. Black

Solution

  1. Step 1: Identify subplot position

    With plt.subplots(2, 2), subplots are indexed as [0,0], [0,1], [1,0], [1,1]. The third subplot is at [1,0].
  2. Step 2: Check plot color argument

    The code sets color='green' for the line in subplot [1,0].
  3. Final Answer:

    Green -> Option C
  4. Quick Check:

    Color set explicitly to green = B [OK]
Hint: Check subplot index and color argument carefully [OK]
Common Mistakes:
  • Assuming default blue color
  • Confusing subplot index order
  • Ignoring color parameter in plot
4. Identify the error in this code that tries to set the y-axis label of the last subplot:
fig, axs = plt.subplots(2, 2)
axs[2, 1].set_ylabel('Y Label')
medium
A. IndexError because axs has shape (2,2) and index (2,1) is out of range
B. AttributeError because set_ylabel is not a valid method
C. SyntaxError due to wrong indexing syntax
D. No error, code runs fine

Solution

  1. Step 1: Check axs shape and indexing

    With plt.subplots(2, 2), axs shape is (2, 2), valid indices are 0 or 1 for each dimension.
  2. Step 2: Identify invalid index

    Indexing axs[2, 1] tries to access third row (index 2), which does not exist, causing IndexError.
  3. Final Answer:

    IndexError because axs has shape (2,2) and index (2,1) is out of range -> Option A
  4. Quick Check:

    Index out of range error = C [OK]
Hint: Check subplot array shape before indexing [OK]
Common Mistakes:
  • Assuming axs has more rows than created
  • Confusing AttributeError with IndexError
  • Using wrong indexing syntax
5. You want to create a 2x2 grid of subplots and set a different background color for each subplot: red, blue, green, and yellow respectively. Which code snippet correctly applies these colors?
hard
A. colors = ['red', 'blue', 'green', 'yellow'] fig, axs = plt.subplots(2, 2) for ax, color in zip(axs.flat, colors): ax.set_facecolor(color)
B. colors = ['red', 'blue', 'green', 'yellow'] fig, axs = plt.subplots(2, 2) for i in range(4): axs[i].set_facecolor(colors[i])
C. colors = ['red', 'blue', 'green', 'yellow'] fig, axs = plt.subplots(2, 2) for i, j in range(2): axs[i, j].set_facecolor(colors[i+j])
D. colors = ['red', 'blue', 'green', 'yellow'] fig, axs = plt.subplots(2, 2) for ax in axs: ax.set_facecolor(colors)

Solution

  1. Step 1: Understand axs structure and iteration

    With plt.subplots(2, 2), axs is a 2D array. Using axs.flat flattens it to 1D for easy iteration.
  2. Step 2: Apply colors correctly

    Using zip(axs.flat, colors) pairs each subplot with a color. Calling ax.set_facecolor(color) sets background color individually.
  3. Final Answer:

    Use axs.flat with zip and set_facecolor for each subplot -> Option A
  4. Quick Check:

    Flatten axs and zip with colors = A [OK]
Hint: Use axs.flat to loop all subplots in order [OK]
Common Mistakes:
  • Trying to index axs as 1D without flattening
  • Using axs as 2D but wrong color indexing
  • Passing full color list to set_facecolor
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard