Subplots for multiple charts in Data Analysis Python - Time & Space Complexity
When we create multiple charts using subplots, we want to know how the time to draw them grows as we add more charts.
We ask: How does the work increase when we add more charts to the subplot?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
n = 5 # number of charts
x = np.linspace(0, 10, 1000)
fig, axs = plt.subplots(n, 1)
for i in range(n):
y = np.sin(x + i)
axs[i].plot(x, y)
plt.show()
This code creates 5 charts stacked vertically, each plotting a sine wave shifted by i.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over
ncharts to plot data. - How many times: Exactly
ntimes, once per chart.
Each new chart adds a similar amount of work to draw the plot.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times the work of one chart |
| 100 | 100 times the work of one chart |
| 1000 | 1000 times the work of one chart |
Pattern observation: The work grows directly with the number of charts added.
Time Complexity: O(n)
This means the time to create all charts grows in a straight line as you add more charts.
[X] Wrong: "Adding more charts won't increase time much because they are drawn together."
[OK] Correct: Each chart requires its own drawing steps, so more charts mean more work.
Understanding how work grows with more charts helps you explain performance when visualizing data in real projects.
What if we plotted all lines on a single chart instead of multiple subplots? How would the time complexity change?
Practice
plt.subplots() in data visualization?Solution
Step 1: Understand the function of
This function creates a figure and a grid of subplots (axes) to hold multiple charts.plt.subplots()Step 2: Recognize the benefit of multiple charts in one figure
Having multiple charts in one figure helps compare data visually side-by-side.Final Answer:
To create multiple charts in one figure for easy comparison -> Option AQuick Check:
Subplots = multiple charts in one figure [OK]
- Thinking it saves images
- Confusing with styling functions
- Assuming it adds titles
Solution
Step 1: Recall the syntax of
The function takes rows and columns as arguments to create a grid.plt.subplots()Step 2: Identify the 2x2 grid syntax
Usingplt.subplots(2, 2)creates 2 rows and 2 columns, making 4 subplots.Final Answer:
fig, axs = plt.subplots(2, 2) -> Option DQuick Check:
Rows and columns = plt.subplots(2, 2) [OK]
- Passing only one number for grid
- Confusing total plots with grid shape
- Using wrong argument order
axs when you run fig, axs = plt.subplots(3, 1)?Solution
Step 1: Understand
This means 3 subplots stacked vertically.plt.subplots(3, 1)creates 3 rows and 1 columnStep 2: Check the type of
When columns = 1,axsfor single columnaxsis a 2D numpy array of shape (3,1) of Axes objects, not a list.Final Answer:
A 3x1 numpy array of Axes objects -> Option CQuick Check:
Single column = 2D numpy array of shape (3,1) [OK]
- Assuming always 1D list
- Confusing rows and columns order
- Expecting single Axes object
fig, axs = plt.subplots(2, 2) axs[2, 0].plot([1, 2, 3])
Solution
Step 1: Check the shape of axs from
It creates a 2x2 grid, so valid indices are 0 or 1 for rows and columns.plt.subplots(2, 2)Step 2: Analyze the index
Index 2 for rows is out of range (only 0 and 1 allowed), causing IndexError.axs[2, 0]Final Answer:
IndexError because axs[2, 0] is out of range -> Option BQuick Check:
Index out of range = IndexError [OK]
- Using indices beyond grid size
- Confusing syntax error with index error
- Assuming axs is a flat list
Solution
Step 1: Understand subplot grid and number of plots
A 2x3 grid has 6 subplots but only 5 are needed, so one remains empty.Step 2: Use
This allows easy indexing over the 2D array of axes.axs.flatto iterate all subplots in 1DStep 3: Turn off the extra subplot axis
Callingaxs.flat[5].axis('off')hides the unused subplot.Final Answer:
fig, axs = plt.subplots(2, 3) for i in range(5): axs.flat[i].plot([i, i+1, i+2]) axs.flat[5].axis('off') -> Option AQuick Check:
Use axs.flat and turn off extra subplot [OK]
- Ignoring extra subplot and leaving it blank
- Indexing axs as 1D when it's 2D without flat
- Creating wrong grid shape for number of plots
Ready to prove it?
Pass the test and this pattern moves unknown
