Showing multiple plots in one figure helps compare data easily. It saves space and makes patterns clearer.
Why multiple plots per figure matter in Matplotlib
Introduction
Syntax
Matplotlib
import matplotlib.pyplot as plt fig, axs = plt.subplots(nrows, ncols) axs[row, col].plot(data_x, data_y) plt.show()
plt.subplots() creates a grid of plots inside one figure.
You can control rows and columns to organize your plots.
Examples
Matplotlib
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()
Matplotlib
fig, axs = plt.subplots(2, 2) axs[0, 0].plot([1, 2], [3, 4]) axs[0, 1].plot([1, 2], [4, 3]) axs[1, 0].plot([1, 2], [2, 5]) axs[1, 1].plot([1, 2], [5, 2]) plt.show()
Sample Program
This program shows two different plots stacked vertically in one figure. It helps compare line and bar charts easily.
Matplotlib
import matplotlib.pyplot as plt # Create 2 rows and 1 column of plots fig, axs = plt.subplots(2, 1) # First plot: simple line axs[0].plot([0, 1, 2], [0, 1, 4]) axs[0].set_title('Line Plot') # Second plot: simple bar axs[1].bar(['A', 'B', 'C'], [3, 7, 5]) axs[1].set_title('Bar Plot') plt.tight_layout() plt.show()
Important Notes
Use plt.tight_layout() to avoid overlapping labels.
Each subplot can have its own title and labels.
Multiple plots in one figure make your data story clearer and save space.
Summary
Multiple plots per figure help compare data side by side.
Use plt.subplots() to create grids of plots.
Each plot can be customized independently inside the figure.
Practice
1. Why is it useful to have multiple plots in one figure when analyzing data?
easy
Solution
Step 1: Understand the purpose of multiple plots
Multiple plots in one figure let us see different data sets or views together.Step 2: Compare data side by side
This side-by-side view helps spot differences or similarities easily.Final Answer:
It allows easy comparison of different data sets side by side. -> Option CQuick 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
Solution
Step 1: Recall the function for multiple plots
Use plt.subplots(rows, columns) to create a grid of plots.Step 2: Match rows and columns
For 2 rows and 3 columns, call plt.subplots(2, 3).Final Answer:
plt.subplots(2, 3) -> Option BQuick 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
Solution
Step 1: Understand plt.subplots(1, 2)
This creates 1 row and 2 columns of plots, so axs is an array of two axes.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.Final Answer:
Two separate plots side by side, each with its own line. -> Option AQuick 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
Solution
Step 1: Check plt.subplots(2) output
It creates 2 subplots in one column, so ax is an array of axes.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.Final Answer:
ax is an array but used as a single axis, causing an error. -> Option AQuick 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
Solution
Step 1: Understand the goal
We want to compare 3 products side by side in one figure for clarity.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.Step 3: Label each plot for clarity
Clear labels help understand which plot shows which product's sales.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 DQuick 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
10 questions ~5 mineasy hard
