Sometimes you want to change how each small plot looks inside a bigger picture. This helps show different details clearly.
Individual subplot customization in 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.
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')
fig, axs = plt.subplots(2, 2) axs[0, 0].set_xlabel('X1') axs[1, 1].set_ylabel('Y2')
fig, axs = plt.subplots(2, 1) axs[0].plot([1, 4, 9], color='red') axs[1].plot([9, 4, 1], color='blue')
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.
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()
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.
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
fig, axs = plt.subplots(2, 2)?Solution
Step 1: Understand subplot indexing
Withplt.subplots(2, 2),axsis a 2x2 array. The second subplot in the first row is at position [0, 1].Step 2: Set title for specific subplot
Useaxs[0, 1].set_title('New Title')to change only that subplot's title.Final Answer:
Useaxs[0, 1].set_title('New Title')-> Option DQuick Check:
Indexing subplots by row and column = A [OK]
- Using axs[1] instead of axs[0, 1]
- Setting title on fig instead of axs
- Using plt.title affects whole figure, not subplot
fig, axs = plt.subplots(3)?Solution
Step 1: Identify subplot indexing in 1D array
Withplt.subplots(3),axsis a 1D array with indices 0, 1, 2. The first subplot is at index 0.Step 2: Use set_xlabel on the correct subplot
Useaxs[0].set_xlabel('X Label')to set the x-axis label of the first subplot.Final Answer:
axs[0].set_xlabel('X Label') -> Option BQuick Check:
Index 0 for first subplot in 1D axs = D [OK]
- Using axs[1] for first subplot
- Calling set_xlabel on fig or axs directly
- Confusing 1D and 2D axs indexing
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()
Solution
Step 1: Identify subplot position
Withplt.subplots(2, 2), subplots are indexed as [0,0], [0,1], [1,0], [1,1]. The third subplot is at [1,0].Step 2: Check plot color argument
The code setscolor='green'for the line in subplot [1,0].Final Answer:
Green -> Option CQuick Check:
Color set explicitly to green = B [OK]
- Assuming default blue color
- Confusing subplot index order
- Ignoring color parameter in plot
fig, axs = plt.subplots(2, 2)
axs[2, 1].set_ylabel('Y Label')Solution
Step 1: Check axs shape and indexing
Withplt.subplots(2, 2), axs shape is (2, 2), valid indices are 0 or 1 for each dimension.Step 2: Identify invalid index
Indexing axs[2, 1] tries to access third row (index 2), which does not exist, causing IndexError.Final Answer:
IndexError because axs has shape (2,2) and index (2,1) is out of range -> Option AQuick Check:
Index out of range error = C [OK]
- Assuming axs has more rows than created
- Confusing AttributeError with IndexError
- Using wrong indexing syntax
Solution
Step 1: Understand axs structure and iteration
Withplt.subplots(2, 2), axs is a 2D array. Usingaxs.flatflattens it to 1D for easy iteration.Step 2: Apply colors correctly
Usingzip(axs.flat, colors)pairs each subplot with a color. Callingax.set_facecolor(color)sets background color individually.Final Answer:
Use axs.flat with zip and set_facecolor for each subplot -> Option AQuick Check:
Flatten axs and zip with colors = A [OK]
- Trying to index axs as 1D without flattening
- Using axs as 2D but wrong color indexing
- Passing full color list to set_facecolor
Ready to prove it?
Pass the test and this pattern moves unknown
