📖 Scenario: You are creating a simple plot with multiple subplots using matplotlib. Sometimes, the default spacing between plots is too wide or overlapping. Using tight layout helps adjust the spacing automatically.
🎯 Goal: Learn how to create multiple subplots and apply tight_layout() to improve spacing between them.
📋 What You'll Learn
Create a figure with 2 rows and 2 columns of subplots using matplotlib
Plot simple line data on each subplot
Use tight_layout() to adjust spacing automatically
Print a message confirming the layout adjustment
💡 Why This Matters
🌍 Real World
When creating reports or dashboards with multiple charts, tight layout helps make the visuals clear and neat without overlapping labels or titles.
💼 Career
Data scientists and analysts often create multiple plots for data exploration and presentation. Knowing how to adjust plot spacing improves the quality of visual reports.
Progress0 / 4 steps
1
Create 2x2 subplots with sample data
Import matplotlib.pyplot as plt. Create a figure and 2x2 subplots using plt.subplots(2, 2). Plot the line [1, 2, 3] on each subplot using ax.plot([1, 2, 3]) in a loop over all axes stored in axes.
Matplotlib
Hint
Use plt.subplots(2, 2) to create 4 subplots and loop over axes.flat to plot on each.
2
Add a configuration variable for spacing padding
Create a variable called padding and set it to 0.5. This will be used later to control spacing padding in tight_layout().
Matplotlib
Hint
Just create a variable named padding and assign it the value 0.5.
3
Apply tight layout with padding
Use fig.tight_layout(pad=padding) to adjust the spacing between subplots using the padding variable.
Matplotlib
Hint
Call fig.tight_layout() with the argument pad=padding.
4
Print confirmation message
Print the message 'Tight layout applied with padding 0.5' to confirm the layout adjustment.
Matplotlib
Hint
Use print() to display the exact message.
Practice
(1/5)
1. What is the main purpose of using tight_layout() in matplotlib?
easy
A. To automatically adjust spacing between subplots to prevent overlap
B. To change the color of the plot background
C. To add grid lines to the plot
D. To save the plot as an image file
Solution
Step 1: Understand the function role
tight_layout() is designed to adjust subplot parameters automatically.
Step 2: Identify the effect on spacing
It prevents labels, titles, and ticks from overlapping by adjusting spacing.
Final Answer:
To automatically adjust spacing between subplots to prevent overlap -> Option A
B. tight_layout() must be called before subplots()
C. The parameter name should be 'pad' not 'padding'
D. plt.show() should be called before tight_layout()
Solution
Step 1: Check parameter names for tight_layout()
The correct parameter to control padding is pad, not padding.
Step 2: Confirm function call order
tight_layout() should be called after creating subplots and before plt.show(), which is correct here.
Final Answer:
The parameter name should be 'pad' not 'padding' -> Option C
Quick Check:
Use pad= not padding= [OK]
Hint: Use 'pad' parameter, not 'padding' [OK]
Common Mistakes:
Using wrong parameter names
Calling tight_layout before creating subplots
Calling plt.show() before tight_layout
5. You have a figure with 3 subplots and long axis labels causing overlap. How can you use tight_layout() to fix this and add extra space on the right side for a legend?
hard
A. Call plt.tight_layout(pad=0) to remove all padding
B. Call plt.tight_layout(rect=[0, 0, 0.85, 1]) to leave space on the right
C. Call plt.tight_layout(w_pad=5, h_pad=5) to increase padding equally
D. Call plt.tight_layout() without parameters; it always reserves legend space
Solution
Step 1: Understand the rect parameter
The rect parameter defines the area for subplots as [left, bottom, right, top] in normalized figure coordinates.
Step 2: Use rect to leave space for legend
Setting rect=[0, 0, 0.85, 1] shrinks subplot area to 85% width, leaving right space for legend.
Final Answer:
Call plt.tight_layout(rect=[0, 0, 0.85, 1]) to leave space on the right -> Option B
Quick Check:
rect shrinks plot area for legend space [OK]
Hint: Use rect to reserve space for legends [OK]
Common Mistakes:
Setting pad=0 removes needed spacing
Assuming tight_layout reserves legend space automatically
Using only w_pad and h_pad without adjusting rect
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.