📖 Scenario: You are working on a simple data visualization project. You want to create a plot with multiple sections (subplots) to compare different data sets side by side.
🎯 Goal: Build a Python script that creates a figure and adds two subplots using add_subplot. Each subplot will later hold a different plot.
📋 What You'll Learn
Create a figure object using plt.figure()
Add a first subplot to the figure using add_subplot(1, 2, 1)
Add a second subplot to the figure using add_subplot(1, 2, 2)
Print the axes objects to confirm creation
💡 Why This Matters
🌍 Real World
Creating multiple plots in one figure helps compare data sets side by side, useful in reports and presentations.
💼 Career
Data scientists and analysts often create multi-plot figures to visualize and compare different data trends clearly.
Progress0 / 4 steps
1
Create a figure object
Write code to create a figure object called fig using plt.figure().
Matplotlib
Hint
Use plt.figure() to create a new figure and assign it to fig.
2
Add the first subplot
Add a subplot to the figure fig using add_subplot(1, 2, 1) and assign it to a variable called ax1.
Matplotlib
Hint
Use fig.add_subplot(1, 2, 1) to add the first subplot and save it as ax1.
3
Add the second subplot
Add a second subplot to the figure fig using add_subplot(1, 2, 2) and assign it to a variable called ax2.
Matplotlib
Hint
Use fig.add_subplot(1, 2, 2) to add the second subplot and save it as ax2.
4
Print the axes objects
Print the variables ax1 and ax2 to display the axes objects created by add_subplot.
Matplotlib
Hint
Use print(ax1) and print(ax2) to show the axes objects.
Practice
(1/5)
1. What does the add_subplot method do in matplotlib?
easy
A. It removes a plot from the figure.
B. It saves the figure to a file.
C. It changes the color of the plot.
D. It adds a new plot (axes) to an existing figure.
Solution
Step 1: Understand the purpose of add_subplot
The add_subplot method is used to add one or more plots (axes) inside a figure in matplotlib.
Step 2: Compare with other options
Saving, coloring, or removing plots are done by other methods, not add_subplot.
Final Answer:
It adds a new plot (axes) to an existing figure. -> Option D
Quick Check:
add_subplot adds axes [OK]
Hint: Remember: add_subplot adds plots inside a figure [OK]
Common Mistakes:
Confusing add_subplot with savefig
Thinking it changes plot colors
Assuming it deletes plots
2. Which of the following is the correct way to create a 2x2 grid of subplots and select the third subplot using add_subplot?
easy
A. fig.add_subplot(2, 2, 3)
B. fig.add_subplot(3, 2, 2)
C. fig.add_subplot(2, 3, 1)
D. fig.add_subplot(2, 2)
Solution
Step 1: Understand the parameters of add_subplot
The method add_subplot(rows, columns, index) creates a grid and selects the subplot at the given index.
Step 2: Match the correct grid and index
For a 2x2 grid, the third subplot is at position 3, so fig.add_subplot(2, 2, 3) is correct.
Final Answer:
fig.add_subplot(2, 2, 3) -> Option A
Quick Check:
2x2 grid, third subplot = add_subplot(2,2,3) [OK]
Hint: Use (rows, cols, position) to pick subplot [OK]
Common Mistakes:
Mixing rows and columns order
Omitting the position argument
Using wrong grid size
3. What will be the output of the following code snippet?
B. Missing the subplot position argument in add_subplot.
C. The plot data lists have different lengths.
D. plt.show() is called before plotting.
Solution
Step 1: Check add_subplot parameters
The method requires three arguments: rows, columns, and position. Here, only two are given.
Step 2: Verify other parts of the code
Plot data lists have equal length, plt.show() is correctly placed, and plt.figure() is valid.
Final Answer:
Missing the subplot position argument in add_subplot. -> Option B
Quick Check:
add_subplot needs 3 args (rows, cols, pos) [OK]
Hint: add_subplot needs three numbers: rows, cols, position [OK]
Common Mistakes:
Forgetting the position argument
Confusing plt.figure() with plt.subplots()
Assuming plt.show() order matters here
5. You want to create a figure with 3 subplots arranged in 1 row and 3 columns. You want to add the second subplot and plot a line on it. Which code snippet correctly does this?