0
0
Matplotlibdata~10 mins

Unequal subplot sizes in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Unequal subplot sizes
Start: Create Figure
Define GridSpec with unequal ratios
Add subplot 1 with larger size
Add subplot 2 with smaller size
Plot data on each subplot
Show figure with unequal subplot sizes
We create a figure, define a grid layout with different size ratios, add subplots accordingly, plot data, and display the figure.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure()
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax1.plot([1,2,3], [1,4,9])
ax2.plot([1,2,3], [1,2,3])
plt.show()
This code creates two subplots side by side with the first subplot three times wider than the second.
Execution Table
StepActionObject CreatedSize/RatioPlot DataOutput
1Create figureFigureFull canvasNoneFigure object ready
2Define GridSpecGridSpec1 row, 2 cols, width_ratios=[3,1]NoneGrid layout set
3Add subplot ax1AxesSubplot ax1Width ratio 3 unitsPlot y=x^2Left large subplot created
4Add subplot ax2AxesSubplot ax2Width ratio 1 unitPlot y=xRight smaller subplot created
5Display figureFigure with 2 subplotsUnequal widthsBoth plots visibleFigure shown on screen
💡 All steps complete, figure displayed with unequal subplot sizes.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
figNoneFigure object createdFigure with GridSpecFigure with 2 subplotsFigure ready to show
gsNoneGridSpec with ratios [3,1]GridSpec unchangedGridSpec unchangedGridSpec unchanged
ax1NoneNoneAxesSubplot with width ratio 3AxesSubplot with width ratio 3AxesSubplot ready
ax2NoneNoneNoneAxesSubplot with width ratio 1AxesSubplot ready
Key Moments - 3 Insights
Why does the first subplot appear wider than the second?
Because the GridSpec was defined with width_ratios=[3,1], the first subplot gets 3 parts width and the second gets 1 part, making the first subplot three times wider (see execution_table step 2 and 3).
What happens if we change width_ratios to [1,3]?
The second subplot becomes wider than the first, reversing their sizes. This is controlled by the width_ratios parameter in GridSpec (see variable_tracker gs after step 2).
Can we have unequal heights instead of widths?
Yes, by using height_ratios in GridSpec for rows instead of width_ratios for columns, you can control vertical sizes similarly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the width ratio of subplot ax1?
A1 unit
B3 units
CEqual width
DUndefined
💡 Hint
Check the 'Size/Ratio' column in execution_table row for step 3.
At which step is the second subplot (ax2) created?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for when ax2 is added.
If width_ratios were changed to [1, 3], how would the subplot sizes change?
ABoth subplots become equal size
BFirst subplot becomes larger, second smaller
CFirst subplot becomes smaller, second larger
DNo change in sizes
💡 Hint
Refer to key_moments explanation about width_ratios effect on subplot sizes.
Concept Snapshot
Use matplotlib.gridspec.GridSpec to create subplots with unequal sizes.
Define width_ratios or height_ratios to control relative sizes.
Add subplots using the GridSpec slices.
Plot data on each subplot.
Call plt.show() to display the figure with unequal subplot sizes.
Full Transcript
This visual execution shows how to create subplots with unequal sizes using matplotlib's GridSpec. First, a figure is created. Then, GridSpec defines a grid layout with specified width ratios, here [3,1], meaning the first subplot is three times wider than the second. Subplots are added according to this grid. Data is plotted on each subplot. Finally, the figure is displayed showing the unequal subplot sizes clearly. Variables like fig, gs, ax1, and ax2 change step-by-step as subplots are created and plotted. Key moments clarify why the first subplot is wider and how changing width_ratios affects sizes. The quiz tests understanding of these steps and ratios.