0
0
Data Analysis Pythondata~10 mins

Subplots for multiple charts in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subplots for multiple charts
Start
Create Figure and Axes with subplots()
Plot chart 1 on Axes 1
Plot chart 2 on Axes 2
Adjust layout if needed
Show or save the figure
End
This flow shows how to create a figure with multiple charts using subplots, plot each chart on its own axes, adjust layout, and display the result.
Execution Sample
Data Analysis Python
import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 2)
axs[0].plot([1, 2, 3], [1, 4, 9])
axs[1].bar([1, 2, 3], [3, 2, 5])
plt.show()
This code creates a figure with two side-by-side charts: a line plot and a bar chart.
Execution Table
StepActionObject Created/ModifiedResult/Output
1Call plt.subplots(1, 2)fig, axsFigure with 1 row, 2 columns of Axes created
2Plot line on axs[0]axs[0]Line plot with points (1,1), (2,4), (3,9) drawn
3Plot bar on axs[1]axs[1]Bar chart with bars at x=1,2,3 and heights 3,2,5 drawn
4Call plt.show()Figure windowDisplays figure with two charts side by side
5End-Execution stops after showing the figure
💡 Figure displayed and script ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
figNoneFigure object createdFigure unchangedFigure unchangedFigure unchanged
axsNoneArray of 2 Axes objectsaxs[0] has line plotaxs[1] has bar chartBoth Axes have their plots
Key Moments - 2 Insights
Why do we use axs[0] and axs[1] to plot instead of just axs?
Because plt.subplots(1, 2) returns an array of Axes objects, each representing a separate chart area. axs[0] is the first chart, axs[1] the second. See execution_table steps 2 and 3.
What happens if we call plt.show() before plotting on axs[1]?
The figure will display only the plots drawn before plt.show(). So axs[1] would be empty. See execution_table step 4 shows all plots after both are drawn.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does axs contain after Step 1?
AA Figure object
BA list of two Axes objects
CA single Axes object
DAn empty list
💡 Hint
Check execution_table row 1: plt.subplots(1, 2) returns fig and axs array
At which step is the bar chart drawn on the figure?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
See execution_table row 3: axs[1] bar chart is drawn
If we change plt.subplots(1, 2) to plt.subplots(2, 1), how does axs change?
Aaxs becomes a 2-element array with vertical layout
Baxs becomes a single Axes object
Caxs becomes a 4-element array
Daxs becomes empty
💡 Hint
plt.subplots(rows, cols) creates rows x cols Axes; changing to (2,1) stacks charts vertically
Concept Snapshot
Use plt.subplots(rows, cols) to create multiple charts in one figure.
It returns a Figure and an array of Axes.
Plot each chart on its Axes (e.g., axs[0], axs[1]).
Call plt.show() to display all charts together.
Adjust layout with plt.tight_layout() if needed.
Full Transcript
This lesson shows how to create multiple charts in one figure using matplotlib's subplots function. First, plt.subplots(1, 2) creates a figure with two side-by-side chart areas, returning a Figure object and an array of two Axes objects. We plot a line chart on the first Axes (axs[0]) and a bar chart on the second (axs[1]). Finally, plt.show() displays the figure with both charts. Variables fig and axs track the figure and axes states. Key points include understanding that axs is an array of Axes, and plotting must be done on each Axes separately before showing the figure.