0
0
Matplotlibdata~10 mins

Individual subplot customization in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Individual subplot customization
Create Figure and Subplots
Access Each Subplot Individually
Apply Custom Settings to Each Subplot
Display Customized Subplots
Start by creating subplots, then access each one to change titles, labels, or styles separately before showing the final figure.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2)
axs[0].set_title('Left Plot')
axs[1].set_title('Right Plot')
plt.show()
This code creates two side-by-side plots and sets different titles for each subplot.
Execution Table
StepActionTarget SubplotEffectOutput
1Create figure and 2 subplotsfig, axsfig with axs[0], axs[1]Two empty plots side by side
2Set title 'Left Plot'axs[0]Title set to 'Left Plot'Left plot shows title 'Left Plot'
3Set title 'Right Plot'axs[1]Title set to 'Right Plot'Right plot shows title 'Right Plot'
4Display figurefigFigure renderedTwo plots visible with individual titles
💡 All subplots customized and figure displayed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
figNoneFigure object createdSameSameSame
axsNone[AxesSubplot(0), AxesSubplot(1)]SameSameSame
axs[0].title.get_text()"""""Left Plot""Left Plot""Left Plot"
axs[1].title.get_text()"""""""Right Plot""Right Plot"
Key Moments - 2 Insights
Why do we use axs[0] and axs[1] instead of just axs?
Because axs is an array of subplot axes, each subplot is accessed individually by its index, as shown in steps 2 and 3 of the execution table.
What happens if we set the title on axs instead of axs[0] or axs[1]?
Setting title on axs (the array) will cause an error or no effect because titles belong to individual subplot axes, not the array container, as seen in the variable tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the title of axs[1] after step 3?
A"" (empty string)
B"Left Plot"
C"Right Plot"
DError
💡 Hint
Check the 'Effect' column in step 3 of the execution table.
At which step does the left subplot get its title set?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Target Subplot' columns in the execution table.
If we add axs[0].set_xlabel('X axis') after step 3, what changes in variable_tracker?
Aaxs[0].xlabel changes from '' to 'X axis'
Baxs[1].xlabel changes from '' to 'X axis'
Cfig variable changes
DNo change
💡 Hint
Variable tracker shows subplot properties changing per step.
Concept Snapshot
Create subplots with plt.subplots()
Access each subplot by index (axs[0], axs[1], ...)
Customize titles, labels, or styles individually
Call plt.show() to display all
Each subplot is an independent Axes object
Full Transcript
We start by creating a figure and multiple subplots using plt.subplots(). This returns a figure and an array of subplot axes. We then access each subplot individually by its index, for example axs[0] for the first subplot and axs[1] for the second. We customize each subplot by setting titles or labels on these individual axes. Finally, we display the figure with plt.show(), which shows all subplots with their individual customizations.