📖 Scenario: You are creating a simple data visualization for a report. You want to show two charts side by side, but one chart should be bigger than the other to highlight its importance.
🎯 Goal: Build a matplotlib figure with two subplots arranged horizontally. The left subplot should be twice as wide as the right subplot.
📋 What You'll Learn
Create a matplotlib figure with two subplots arranged in one row
Use gridspec_kw to set the width ratios of the subplots to 2 and 1
Plot simple line charts on each subplot
Display the figure with plt.show()
💡 Why This Matters
🌍 Real World
In reports or presentations, some charts need more space to show details clearly. Unequal subplot sizes help highlight important data visually.
💼 Career
Data analysts and scientists often customize plots to communicate insights effectively. Knowing how to adjust subplot sizes is a useful skill.
Progress0 / 4 steps
1
Create sample data
Create two lists called x and y1 with values [1, 2, 3, 4, 5] and [1, 4, 9, 16, 25] respectively.
Matplotlib
Hint
Use square brackets to create lists. For example, x = [1, 2, 3].
2
Add second data list
Create a list called y2 with values [25, 16, 9, 4, 1].
Matplotlib
Hint
Remember to use square brackets and commas to separate values.
3
Create figure with unequal subplot sizes
Import matplotlib.pyplot as plt. Create a figure and two subplots in one row using plt.subplots() with nrows=1, ncols=2, and gridspec_kw={'width_ratios': [2, 1]}. Plot x vs y1 on the first subplot and x vs y2 on the second subplot.
Matplotlib
Hint
Use plt.subplots() with gridspec_kw to set different widths for subplots.
4
Display the figure
Add a line to display the figure using plt.show().
Matplotlib
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. What is the main advantage of using unequal subplot sizes in matplotlib?
easy
A. To automatically adjust colors of the plots
B. To make all charts the same size for uniformity
C. To highlight important charts by giving them more space
D. To reduce the total number of charts displayed
Solution
Step 1: Understand subplot size purpose
Unequal subplot sizes allow some plots to be larger, drawing more attention.
Step 2: Compare options
Only To highlight important charts by giving them more space mentions highlighting important charts by size, which is the main advantage.
Final Answer:
To highlight important charts by giving them more space -> Option C
Quick Check:
Unequal sizes highlight key charts [OK]
Hint: Unequal sizes = emphasize important plots [OK]
Common Mistakes:
Thinking all subplots must be equal size
Confusing size with color or number of plots
Assuming unequal sizes reduce charts
2. Which of the following is the correct way to create subplots with unequal sizes using gridspec_kw in plt.subplots()?
easy
A. plt.subplots(1, 2, gridspec_kw={'width_ratios': [2, 1]})
B. plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 2]})
C. plt.subplots(1, 2, gridspec_kw={'height_ratios': [2, 1]})
D. plt.subplots(2, 1, gridspec_kw={'width_ratios': [1, 2]})
Solution
Step 1: Identify correct parameter for horizontal subplots
For 1 row and 2 columns, use 'width_ratios' to set horizontal size ratios.
Step 2: Match ratios to subplot layout
plt.subplots(1, 2, gridspec_kw={'width_ratios': [2, 1]}) uses 1 row, 2 columns with width_ratios, which is correct syntax.
Final Answer:
plt.subplots(1, 2, gridspec_kw={'width_ratios': [2, 1]}) -> Option A