We use add_subplot to add one or more plots inside a figure. It helps organize multiple graphs in rows and columns.
Axes creation with add_subplot in Matplotlib
Introduction
Syntax
Matplotlib
ax = fig.add_subplot(nrows, ncols, index) # or using a 3-digit code ax = fig.add_subplot(abc) # where a=nrows, b=ncols, c=index
nrows and ncols define the grid size.
index is the position of the subplot (starts at 1).
Examples
Matplotlib
fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) # top-left plot ax2 = fig.add_subplot(2, 2, 2) # top-right plot
111 means 1 row, 1 column, 1st plot (just one plot).Matplotlib
fig = plt.figure() ax = fig.add_subplot(111) # single plot in 1x1 grid
Matplotlib
fig = plt.figure() ax = fig.add_subplot(3, 1, 2) # middle plot in 3 rows, 1 column
Sample Program
This code creates a figure with 4 plots arranged in 2 rows and 2 columns. Each plot shows a simple line graph with a title.
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure(figsize=(6, 4)) # Create a 2x2 grid of plots ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 3) ax4 = fig.add_subplot(2, 2, 4) # Plot simple lines in each subplot ax1.plot([1, 2, 3], [1, 4, 9]) ax1.set_title('Plot 1') ax2.plot([1, 2, 3], [2, 3, 4]) ax2.set_title('Plot 2') ax3.plot([1, 2, 3], [5, 2, 1]) ax3.set_title('Plot 3') ax4.plot([1, 2, 3], [3, 6, 9]) ax4.set_title('Plot 4') plt.tight_layout() plt.show()
Important Notes
The index in add_subplot starts at 1, not 0.
You can use plt.subplots() for a simpler way to create multiple axes, but add_subplot gives more control.
Remember to call plt.show() to display the figure.
Summary
add_subplot helps add one or more plots inside a figure in a grid layout.
You specify the grid size and position to control where each plot appears.
This is useful for comparing multiple charts in one window.
Practice
1. What does the
add_subplot method do in matplotlib?easy
Solution
Step 1: Understand the purpose of add_subplot
Theadd_subplotmethod 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, notadd_subplot.Final Answer:
It adds a new plot (axes) to an existing figure. -> Option DQuick 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
Solution
Step 1: Understand the parameters of add_subplot
The methodadd_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, sofig.add_subplot(2, 2, 3)is correct.Final Answer:
fig.add_subplot(2, 2, 3) -> Option AQuick 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?
import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122) print(type(ax1), type(ax2))
medium
Solution
Step 1: Understand add_subplot with 3-digit code
The code121means 1 row, 2 columns, subplot 1;122means subplot 2.Step 2: Check the returned object types
Bothax1andax2are subplot axes objects of typeAxesSubplot.Final Answer:
<class 'matplotlib.axes._subplots.AxesSubplot'> <class 'matplotlib.axes._subplots.AxesSubplot'> -> Option AQuick Check:
add_subplot returns AxesSubplot objects [OK]
Hint: 3-digit code means (rows, cols, index) [OK]
Common Mistakes:
- Expecting Figure type instead of Axes
- Confusing syntax with tuple input
- Thinking it causes syntax error
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(2, 2) ax.plot([1, 2, 3], [4, 5, 6]) plt.show()
medium
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 BQuick 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?
hard
Solution
Step 1: Understand subplot layout
We want 1 row and 3 columns, so the call isadd_subplot(1, 3, position).Step 2: Select the second subplot and plot
Position 2 selects the second subplot. The plot command is correct.Step 3: Check other options
fig, ax = plt.subplots(3, 1) ax[1].plot([0, 1, 2], [2, 1, 0]) plt.show() uses plt.subplots with wrong shape, C uses wrong layout, D uses invalid position 3 in 1x2 grid.Final Answer:
fig = plt.figure() ax = fig.add_subplot(1, 3, 2) ax.plot([0, 1, 2], [2, 1, 0]) plt.show() -> Option CQuick Check:
1 row, 3 cols, second subplot = add_subplot(1,3,2) [OK]
Hint: Match rows, cols, and position carefully [OK]
Common Mistakes:
- Mixing rows and columns numbers
- Using wrong subplot index
- Confusing plt.figure() and plt.subplots()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
