What is the purpose of customizing individual subplots in matplotlib?
Customizing individual subplots allows you to change titles, labels, colors, and other properties separately for each plot, making your visualization clearer and more informative.
Click to reveal answer
beginner
How do you access a specific subplot to customize it when using plt.subplots()?
You get an array or list of axes objects from plt.subplots(). You can access each subplot by indexing this array, for example, axes[0, 1] for the subplot in the first row, second column.
Click to reveal answer
beginner
Which matplotlib method is used to set the title of an individual subplot?
The method is set_title(). For example, axes[0].set_title('My Title') sets the title of the first subplot.
Click to reveal answer
beginner
How can you change the x-axis label of a specific subplot?
Use the set_xlabel() method on the subplot's axes object. For example, axes[1].set_xlabel('Time (s)') changes the x-axis label of the second subplot.
Click to reveal answer
beginner
True or False: You can customize the color and line style of each subplot independently.
True. Each subplot is an independent axes object, so you can customize colors, line styles, markers, and more separately for each one.
Click to reveal answer
How do you get the axes object for the subplot in the second row, first column when using plt.subplots(2, 2)?
Aaxes[2, 1]
Baxes[0, 1]
Caxes[1]
Daxes[1, 0]
✗ Incorrect
In a 2x2 grid, indexing starts at 0. The second row is index 1, and the first column is index 0, so axes[1, 0] accesses that subplot.
Which method changes the y-axis label of a single subplot?
Aset_ylabel()
Bset_xlabel()
Cset_title()
Dset_legend()
✗ Incorrect
The method set_ylabel() changes the y-axis label of a subplot.
If you want to set a different color for each subplot's line, what should you do?
ASet a global color before creating subplots
BCustomize the color in the plot call for each axes object
CUse plt.color() once
DColors cannot be changed per subplot
✗ Incorrect
Each subplot is independent, so you set the color when plotting on each axes object.
What does plt.subplots() return?
AA list of plot titles
BOnly a figure object
CA figure and an array of axes objects
DOnly an axes object
✗ Incorrect
plt.subplots() returns a figure and an array (or single) axes objects for subplots.
How can you add a grid only to the first subplot?
Aaxes[0].grid(True)
Bplt.grid(True)
Cfig.grid(True)
Dgrid() applies to all subplots automatically
✗ Incorrect
Calling grid(True) on the specific axes object adds a grid only to that subplot.
Explain how to customize the title, x-label, and line color of individual subplots in matplotlib.
Think about how you get each subplot and then call methods on it.
You got /4 concepts.
Describe the steps to create a 2x2 grid of subplots and customize each subplot differently.
Start from creating subplots, then access each to customize.
You got /4 concepts.
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
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].
Step 2: Set title for specific subplot
Use axs[0, 1].set_title('New Title') to change only that subplot's title.
Final Answer:
Use axs[0, 1].set_title('New Title') -> Option D
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
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.
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.
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
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.
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 A
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
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.
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.
Final Answer:
Use axs.flat with zip and set_facecolor for each subplot -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.