Bird
Raised Fist0
Matplotlibdata~10 mins

Why multiple plots per figure matter in Matplotlib - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why multiple plots per figure matter
Start: Create Figure
↓
Add Subplot 1
→Plot Data 1
↓
Add Subplot 2
→Plot Data 2
↓
Add Subplot 3
→Plot Data 3
↓
Adjust Layout
↓
Show Figure with Multiple Plots
↓
End
We start by creating a figure, then add multiple subplots to it, each with its own data. Finally, we adjust layout and display all plots together.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].plot([1, 2, 3], [1, 2, 3])
plt.show()
This code creates a figure with 4 subplots arranged in 2 rows and 2 columns, then plots different data on two of them and shows the figure.
Execution Table
StepActionResultFigure State
1Create figure and 2x2 subplotsFigure with 4 empty subplots created4 empty plots in grid (2 rows x 2 cols)
2Plot data [1,4,9] on subplot (0,0)Line plot appears on top-left subplotTop-left subplot has line plot, others empty
3Plot data [1,2,3] on subplot (0,1)Line plot appears on top-right subplotTop-left and top-right subplots have plots, bottom two empty
4Show figureFigure window opens showing 2 plots in 4-subplot gridAll subplots visible; 2 with data, 2 empty
5EndExecution completeFigure displayed with multiple plots
💡 All plotting commands executed; figure shown with multiple plots in one window
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
figNoneFigure object with 4 subplotsSameSameSame
axsNone2x2 array of subplot axesTop-left subplot has plotTop-left and top-right subplots have plotsSame
Key Moments - 2 Insights
Why do we use multiple subplots instead of separate figures?
Using multiple subplots in one figure groups related plots together for easy comparison, as shown in execution_table steps 2 and 3 where plots appear side by side.
What happens if we plot on the wrong subplot index?
Plotting on a wrong subplot index will place the plot in an unexpected location or cause an error; execution_table step 2 and 3 show correct indexing for top-left and top-right plots.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does the figure contain after Step 3?
ATwo plots on the top row subplots
BOnly one plot on the top-left subplot
CFour plots, one in each subplot
DNo plots, all subplots empty
💡 Hint
Refer to the 'Figure State' column in Step 3 of execution_table
At which step does the figure window open to show the plots?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Action' column for when plt.show() is called in execution_table
If we add a plot to axs[1,1], how would the variable 'axs' change after Step 3?
AIt would cause an error
BIt would remain unchanged with two plots
CIt would have three subplots with plots
DIt would remove existing plots
💡 Hint
Look at variable_tracker for how 'axs' updates when plots are added
Concept Snapshot
Use plt.subplots(rows, cols) to create multiple plots in one figure.
Each subplot is accessed by axs[row, col].
Plot data on each subplot separately.
Shows related data together for easy comparison.
Call plt.show() once to display all plots.
Full Transcript
This visual execution shows how to create multiple plots in one figure using matplotlib. First, a figure with a grid of subplots is created. Then, data is plotted on specific subplots by indexing the axes array. The figure is displayed with all plots visible together. This helps compare data side by side in one window. Variables 'fig' and 'axs' track the figure and subplot axes. Key moments include why grouping plots matters and how subplot indexing works. The quiz tests understanding of figure state and plotting steps.

Practice

(1/5)
1. Why is it useful to have multiple plots in one figure when analyzing data?
easy
A. It automatically cleans the data for errors.
B. It reduces the total number of data points shown.
C. It allows easy comparison of different data sets side by side.
D. It makes the figure file size smaller.

Solution

  1. Step 1: Understand the purpose of multiple plots

    Multiple plots in one figure let us see different data sets or views together.
  2. Step 2: Compare data side by side

    This side-by-side view helps spot differences or similarities easily.
  3. Final Answer:

    It allows easy comparison of different data sets side by side. -> Option C
  4. Quick Check:

    Multiple plots = easy comparison [OK]
Hint: Multiple plots show data side by side for easy comparison [OK]
Common Mistakes:
  • Thinking multiple plots reduce data points
  • Believing multiple plots clean data automatically
  • Assuming multiple plots reduce file size
2. Which of the following is the correct way to create a figure with 2 rows and 3 columns of plots using matplotlib?
easy
A. plt.subplots(3, 2)
B. plt.subplots(2, 3)
C. plt.subplot(2, 3)
D. plt.figure(2, 3)

Solution

  1. Step 1: Recall the function for multiple plots

    Use plt.subplots(rows, columns) to create a grid of plots.
  2. Step 2: Match rows and columns

    For 2 rows and 3 columns, call plt.subplots(2, 3).
  3. Final Answer:

    plt.subplots(2, 3) -> Option B
  4. Quick Check:

    plt.subplots(rows, cols) = plt.subplots(2, 3) [OK]
Hint: Use plt.subplots(rows, columns) to create grid [OK]
Common Mistakes:
  • Confusing plt.subplot with plt.subplots
  • Swapping rows and columns
  • Using plt.figure for subplots
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2)
axs[0].plot([1, 2, 3], [4, 5, 6])
axs[1].plot([1, 2, 3], [6, 5, 4])
plt.show()
medium
A. Two separate plots side by side, each with its own line.
B. A blank figure with no plots.
C. An error because axs is not iterable.
D. A single plot with two lines overlapping.

Solution

  1. Step 1: Understand plt.subplots(1, 2)

    This creates 1 row and 2 columns of plots, so axs is an array of two axes.
  2. Step 2: Plot on each axis separately

    axs[0] and axs[1] each get their own line plot, so two plots appear side by side.
  3. Final Answer:

    Two separate plots side by side, each with its own line. -> Option A
  4. Quick Check:

    plt.subplots(1,2) + axs[0], axs[1] plots = two side-by-side plots [OK]
Hint: plt.subplots(1,2) creates two side-by-side plots [OK]
Common Mistakes:
  • Thinking axs is a single axis, not an array
  • Expecting one plot with two lines
  • Assuming code causes error
4. Identify the error in this code that tries to create 2 plots in one figure:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2)
ax.plot([1, 2, 3], [4, 5, 6])
plt.show()
medium
A. ax is an array but used as a single axis, causing an error.
B. plt.subplots(2) creates only one plot, so ax.plot is invalid.
C. The plot data lists have different lengths causing error.
D. plt.show() is missing and causes no output.

Solution

  1. Step 1: Check plt.subplots(2) output

    It creates 2 subplots in one column, so ax is an array of axes.
  2. Step 2: Using ax.plot() on array causes error

    ax.plot() expects a single axis, but ax is an array, so this causes an AttributeError.
  3. Final Answer:

    ax is an array but used as a single axis, causing an error. -> Option A
  4. Quick Check:

    Multiple subplots return array, must index before plot [OK]
Hint: Multiple subplots return array; index before plotting [OK]
Common Mistakes:
  • Calling plot on array of axes instead of one axis
  • Assuming plt.subplots(2) returns single axis
  • Ignoring plt.show() is present
5. You want to compare sales data for 3 products over 4 quarters in one figure. Which approach best uses multiple plots per figure to achieve this?
hard
A. Plot all products on a single plot with overlapping lines and no labels.
B. Use plt.figure() once and call plt.plot() three times without subplots.
C. Create 3 separate figures, each with one product's sales plot.
D. Create a 1x3 grid of plots using plt.subplots(1, 3), plot each product's quarterly sales separately, and label each plot.

Solution

  1. Step 1: Understand the goal

    We want to compare 3 products side by side in one figure for clarity.
  2. Step 2: Use plt.subplots(1, 3) for side-by-side plots

    This creates 3 plots in one row, perfect for comparing products separately but together.
  3. Step 3: Label each plot for clarity

    Clear labels help understand which plot shows which product's sales.
  4. Final Answer:

    Create a 1x3 grid of plots using plt.subplots(1, 3), plot each product's quarterly sales separately, and label each plot. -> Option D
  5. Quick Check:

    Multiple plots + labels = clear side-by-side comparison [OK]
Hint: Use plt.subplots(1,3) and label each plot for clear comparison [OK]
Common Mistakes:
  • Plotting all lines on one plot without labels
  • Creating separate figures instead of subplots
  • Not labeling plots causing confusion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard