0
0
Matplotlibdata~10 mins

Shared axes between subplots in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shared axes between subplots
Create Figure
Create Subplots with shared axes
Plot data on each subplot
Axes share limits and ticks
Display combined plot
This flow shows creating a figure with multiple subplots that share x or y axes, so zooming or panning one affects the others.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 1, sharex=True)
axs[0].plot([1, 2, 3], [1, 4, 9])
axs[1].plot([1, 2, 3], [2, 3, 4])
plt.show()
Creates two vertical subplots sharing the x-axis and plots different data on each.
Execution Table
StepActionResultNotes
1Call plt.subplots(2,1, sharex=True)Figure with 2 subplots createdSubplots share x-axis limits and ticks
2Plot on axs[0]Line plotted on top subplotX-axis range set to [1,3], y-axis auto-scaled
3Plot on axs[1]Line plotted on bottom subplotX-axis shared with top subplot
4Display plot with plt.show()Window opens showing 2 plots stacked verticallyX-axis ticks aligned and zoom/pan synced
5User zooms x-axis on one subplotBoth subplots update x-axis rangeShared axis behavior confirmed
6User zooms y-axis on one subplotOnly that subplot y-axis changesY-axis not shared, independent scaling
7Execution endsPlot window remains open until closedEnd of script
💡 Plot window stays open until user closes it, script ends after plt.show()
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
figNoneFigure object createdSameSameSame
axsNoneArray of 2 Axes objectsaxs[0] has line plottedaxs[1] has line plottedSame
x-axis limitsNone[1,3][1,3][1,3][1,3] shared
y-axis limits axs[0]NoneAutoAuto scaled to [1,9]SameSame
y-axis limits axs[1]NoneAutoAutoAuto scaled to [2,4]Same
Key Moments - 2 Insights
Why do both subplots update their x-axis when I zoom one but not the y-axis?
Because sharex=True links the x-axes of both subplots, so zooming one updates both. The y-axes are independent since sharey was not set, so only the zoomed subplot's y-axis changes. See execution_table rows 5 and 6.
What happens if I set sharey=True instead of sharex=True?
Then the y-axes will be linked and zooming or panning vertically on one subplot updates both y-axes. The x-axes remain independent. This is the opposite of the current example where only x is shared.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the x-axis limit after plotting on both subplots?
A[1, 9]
B[0, 10]
C[1, 3]
DAuto-scaled independently
💡 Hint
Check the 'x-axis limits' row in variable_tracker after Step 3
At which step does the bottom subplot get its line plotted?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for plotting on axs[1]
If sharex=False, how would the x-axis behavior change when zooming one subplot?
ABoth subplots zoom together
BOnly the zoomed subplot's x-axis changes
CNeither subplot zooms
DBoth subplots zoom but y-axis stays fixed
💡 Hint
Recall that sharex=True links x-axes; without it, axes are independent
Concept Snapshot
Use plt.subplots(rows, cols, sharex=True/False, sharey=True/False) to create subplots that share axes.
Shared axes sync limits and ticks between subplots.
Zooming or panning one shared axis updates all linked subplots.
Unshared axes remain independent.
Common for comparing data with aligned scales.
Full Transcript
This lesson shows how to create multiple subplots in matplotlib that share axes. We start by creating a figure with two vertical subplots sharing the x-axis using plt.subplots(2,1,sharex=True). We plot different lines on each subplot. Because the x-axis is shared, zooming or panning the x-axis on one subplot updates the other automatically. The y-axes are independent, so zooming vertically affects only the subplot zoomed. The execution table traces each step: creating subplots, plotting lines, displaying the figure, and user interactions. The variable tracker shows how axes limits change. Key moments clarify why shared axes sync only the specified axis. The quiz tests understanding of axis limits, plotting steps, and behavior changes if sharing is disabled. This helps beginners see how shared axes work visually and practically.