Why multiple plots per figure matter in Matplotlib - Performance Analysis
When we create multiple plots in one figure using matplotlib, the time it takes to draw them can change. We want to understand how adding more plots affects the work the computer does.
How does the time to draw grow as we add more plots?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=3, ncols=3)
for ax in axs.flat:
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()
This code creates a 3 by 3 grid of plots and draws the same simple line plot in each subplot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing a plot inside each subplot.
- How many times: Once for each subplot, here 9 times (3 rows x 3 columns).
Each new subplot adds a similar amount of work to draw its plot.
| Input Size (number of plots) | Approx. Operations |
|---|---|
| 1 | 1 unit of drawing work |
| 10 | 10 units of drawing work |
| 100 | 100 units of drawing work |
Pattern observation: The total work grows directly with the number of plots added.
Time Complexity: O(n)
This means if you double the number of plots, the time to draw roughly doubles too.
[X] Wrong: "Adding more plots won't affect drawing time much because each plot is small."
[OK] Correct: Each plot requires separate drawing steps, so more plots add more work linearly.
Understanding how adding plots affects drawing time helps you write efficient visualization code. This skill shows you can think about performance, not just making plots.
What if we changed the plots to be more complex with many lines each? How would the time complexity change?
Practice
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]
- Thinking multiple plots reduce data points
- Believing multiple plots clean data automatically
- Assuming multiple plots reduce file size
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]
- Confusing plt.subplot with plt.subplots
- Swapping rows and columns
- Using plt.figure for subplots
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()
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]
- Thinking axs is a single axis, not an array
- Expecting one plot with two lines
- Assuming code causes error
import matplotlib.pyplot as plt fig, ax = plt.subplots(2) ax.plot([1, 2, 3], [4, 5, 6]) plt.show()
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]
- Calling plot on array of axes instead of one axis
- Assuming plt.subplots(2) returns single axis
- Ignoring plt.show() is present
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]
- Plotting all lines on one plot without labels
- Creating separate figures instead of subplots
- Not labeling plots causing confusion
Ready to prove it?
Pass the test and this pattern moves unknown
