Subplots for multiple charts in Pandas - Time & Space Complexity
When we create multiple charts using subplots in pandas, we want to know how the time to draw them changes as we add more charts.
We ask: How does the work grow when we increase the number of charts?
Analyze the time complexity of the following code snippet.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame({
'A': range(100),
'B': range(100, 200),
'C': range(200, 300)
})
fig, axes = plt.subplots(3, 1)
for i, col in enumerate(data.columns):
data[col].plot(ax=axes[i])
plt.show()
This code creates three line charts stacked vertically, one for each column in the data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each column to draw a chart.
- How many times: Once for each column (number of charts).
Each new chart adds roughly the same amount of work to draw it.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 charts | 3 times the work of one chart |
| 10 charts | 10 times the work of one chart |
| 100 charts | 100 times the work of one chart |
Pattern observation: The work grows directly in proportion to the number of charts.
Time Complexity: O(n)
This means if you double the number of charts, the time to draw them roughly doubles.
[X] Wrong: "Drawing multiple charts together is as fast as drawing just one chart."
[OK] Correct: Each chart requires its own drawing steps, so more charts mean more work and more time.
Understanding how drawing multiple charts scales helps you explain performance in data visualization tasks clearly and confidently.
What if we changed the code to plot all columns on the same single chart instead of separate subplots? How would the time complexity change?
Practice
subplots in pandas plotting?Solution
Step 1: Understand what subplots do
Subplots allow multiple charts to be shown together in one figure.Step 2: Compare options
Only To display multiple charts in one figure for easy comparison describes this purpose correctly.Final Answer:
To display multiple charts in one figure for easy comparison -> Option AQuick Check:
Subplots = multiple charts in one figure [OK]
- Thinking subplots save images automatically
- Confusing subplots with styling options
- Believing subplots only affect one chart
Solution
Step 1: Recall plt.subplots syntax
The function plt.subplots(rows, columns) creates a grid of plots.Step 2: Match the 2x2 grid
Using plt.subplots(2, 2) creates 2 rows and 2 columns, making 4 subplots.Final Answer:
fig, axes = plt.subplots(2, 2) -> Option DQuick Check:
plt.subplots(2, 2) = 2 rows, 2 columns [OK]
- Using plt.subplots(4) creates 4 rows, not a 2x2 grid
- Using keyword arguments incorrectly
- Confusing rows and columns parameters
axes be?import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 3) print(type(axes))
Solution
Step 1: Understand plt.subplots output for multiple plots
When creating multiple subplots in one row, axes is a numpy array of AxesSubplot objects.Step 2: Check the printed type
Printing type(axes) shows numpy.ndarray because axes is an array of subplot axes.Final Answer:
<class 'numpy.ndarray'> -> Option AQuick Check:
Multiple subplots = axes is numpy array [OK]
- Thinking axes is a single AxesSubplot object
- Assuming axes is a list or tuple
- Confusing axes with figure object
fig, axes = plt.subplots(1) axes[0].plot([1, 2, 3]) axes[1].plot([4, 5, 6])
Solution
Step 1: Understand plt.subplots(1) output
plt.subplots(1) creates a single subplot (1 row, 1 column), soaxesis a singleAxesSubplotobject, not an array.Step 2: Indexing causes error
Tryingaxes[0]oraxes[1]on a singleAxesSubplotraisesTypeError: 'AxesSubplot' object is not subscriptable.Final Answer:
axes is a single AxesSubplot, so indexing causes an error -> Option BQuick Check:
plt.subplots(1) = single axes, indexing fails [OK]
- Assuming axes is always a single plot
- Forgetting axes can be array for multiple subplots
- Confusing number of arguments with subplot shape
Solution
Step 1: Determine subplot layout for side-by-side plots
Side-by-side means 1 row and 3 columns, so plt.subplots(1, 3) is correct.Step 2: Check plotting loop and axes usage
Looping over columns and plotting on axes[i] is correct usage.Step 3: Identify errors in other options
fig, axes = plt.subplots(3, 1) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i]) uses (3,1) which is vertical layout, not side-by-side. fig, axes = plt.subplots(1, 3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=fig[i]) uses fig[i] which is invalid. fig, axes = plt.subplots(3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i]) uses plt.subplots(3) which creates 3 rows and 1 column, not side-by-side.Final Answer:
fig, axes = plt.subplots(1, 3) for i, col in enumerate(df.columns[:3]): df[col].plot(ax=axes[i]) -> Option CQuick Check:
Side-by-side = plt.subplots(1, 3) + axes[i] [OK]
- Using wrong subplot shape for layout
- Passing fig instead of axes to plot
- Confusing rows and columns in plt.subplots
Ready to prove it?
Pass the test and this pattern moves unknown
