📖 Scenario: You are creating a simple report with multiple charts side by side. To make the report look neat, you need to adjust the space between the charts.
🎯 Goal: Learn how to create multiple subplots using matplotlib and adjust the spacing between them using plt.subplots_adjust().
📋 What You'll Learn
Create a figure with 2 subplots side by side
Set the horizontal space between subplots to 0.5
Display the figure with adjusted spacing
💡 Why This Matters
🌍 Real World
Adjusting subplot spacing helps make charts in reports or presentations look neat and easy to read.
💼 Career
Data scientists and analysts often create multiple charts together and need to control layout for better visualization.
Progress0 / 4 steps
1
Create two subplots side by side
Write code to create a figure and two subplots side by side using plt.subplots() with nrows=1 and ncols=2. Store the result in variables fig and axes.
Matplotlib
Hint
Use plt.subplots(nrows=1, ncols=2) to create two subplots in one row.
2
Set horizontal spacing between subplots
Create a variable called hspace and set it to 0.5. This will control the horizontal space between the subplots.
Matplotlib
Hint
Just create a variable named hspace and assign it the value 0.5.
3
Adjust subplot spacing using plt.subplots_adjust()
Use plt.subplots_adjust() with the argument wspace=hspace to set the horizontal space between the subplots.
Matplotlib
Hint
Call plt.subplots_adjust() and pass wspace=hspace to adjust horizontal spacing.
4
Display the figure
Use plt.show() to display the figure with the adjusted subplot spacing.
Matplotlib
Hint
Call plt.show() to open the window showing your subplots.
Practice
(1/5)
1. What does the plt.subplots_adjust() function do in matplotlib?
easy
A. It changes the space between subplots in a figure.
B. It creates new subplots in a figure.
C. It saves the figure to a file.
D. It changes the color of the plot lines.
Solution
Step 1: Understand the purpose of plt.subplots_adjust()
This function is used to control the spacing between subplots in a matplotlib figure.
Step 2: Compare options
Only It changes the space between subplots in a figure. correctly describes adjusting space between subplots. Other options describe different functions.
Final Answer:
It changes the space between subplots in a figure. -> Option A
wspace controls horizontal spacing, which is irrelevant here because subplots are vertical. Only hspace affects vertical spacing.
Final Answer:
Using wspace when subplots are stacked vertically. -> Option B
Quick Check:
wspace for horizontal, not vertical spacing [OK]
Hint: Match wspace to horizontal, hspace to vertical layout [OK]
Common Mistakes:
Using wspace for vertical subplot spacing
Ignoring subplot grid shape
Assuming hspace is invalid for vertical layout
5. You have a 2x2 grid of subplots with overlapping x-axis labels. Which plt.subplots_adjust() call best reduces overlap by increasing vertical space and adding some horizontal space?
hard
A. plt.subplots_adjust(wspace=1.5, hspace=0.0)
B. plt.subplots_adjust(wspace=0.0, hspace=0.0)
C. plt.subplots_adjust(wspace=0.0, hspace=1.5)
D. plt.subplots_adjust(wspace=0.4, hspace=0.6)
Solution
Step 1: Understand the problem of overlapping x-axis labels
Overlapping x-axis labels usually happen vertically between rows, so increasing vertical space (hspace) helps.
Step 2: Consider horizontal space
Adding some horizontal space (wspace) also improves readability between columns.
Step 3: Evaluate options
plt.subplots_adjust(wspace=0.4, hspace=0.6) increases both wspace and hspace moderately, which best reduces overlap without wasting space.
Final Answer:
plt.subplots_adjust(wspace=0.4, hspace=0.6) -> Option D
Quick Check:
Increase hspace and wspace moderately to reduce overlap [OK]
Hint: Increase hspace for vertical gaps, wspace for horizontal gaps [OK]
Common Mistakes:
Setting spacing to zero causing overlap
Increasing only one spacing dimension
Using too large spacing causing wasted space
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.