Area chart with plt.fill_between in Matplotlib - Time & Space Complexity
We want to understand how the time to draw an area chart changes as we add more data points.
How does the drawing time grow when the data size grows?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 1000)
y1 = np.sin(x)
y2 = np.sin(x) + 1
plt.fill_between(x, y1, y2)
plt.show()
This code draws an area chart by filling the space between two curves defined by y1 and y2 over x.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over the data points in arrays x, y1, and y2 to fill the area.
- How many times: Once for each data point, so as many times as the length of x (here 1000).
As the number of data points increases, the number of operations to fill the area grows roughly the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 fill steps |
| 100 | About 100 fill steps |
| 1000 | About 1000 fill steps |
Pattern observation: The work grows linearly as the number of points increases.
Time Complexity: O(n)
This means the time to draw the area grows directly in proportion to the number of data points.
[X] Wrong: "Adding more points won't affect drawing time much because it's just one fill call."
[OK] Correct: The fill_between function processes each data point to create the filled area, so more points mean more work.
Understanding how plotting functions scale with data size helps you write efficient code and explain performance clearly.
"What if we used fewer points but more complex shapes? How would the time complexity change?"
Practice
plt.fill_between function do in matplotlib?Solution
Step 1: Understand the purpose of plt.fill_between
The functionplt.fill_betweenis designed to fill the area between two lines or between a line and a baseline, creating an area chart effect.Step 2: Compare with other plot types
Scatter plots, bar charts, and grid lines are created by different functions likeplt.scatter,plt.bar, andplt.grid, notplt.fill_between.Final Answer:
It fills the area between two curves or a curve and a baseline. -> Option AQuick Check:
plt.fill_between fills area under curve [OK]
- Confusing fill_between with scatter or bar plot functions
- Thinking it adds grid lines
- Assuming it only fills under a single line without baseline
Solution
Step 1: Check the required parameters of plt.fill_between
The function requires at least two arrays: x (horizontal axis) and y (vertical values). plt.fill_between(x, y, color='blue') correctly usesplt.fill_between(x, y, color='blue')which is valid syntax with an optional color argument.Step 2: Identify incorrect options
plt.fill(x, y)uses the incorrect functionplt.fillinstead ofplt.fill_between.plt.fill_between(y, x)reverses the x and y parameters, which is incorrect.plt.fill_between(x)misses the y values, causing an error.Final Answer:
plt.fill_between(x, y, color='blue') -> Option BQuick Check:
Correct syntax includes x, y, and optional color [OK]
- Using plt.fill instead of plt.fill_between
- Swapping x and y parameters
- Omitting y values
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [1, 3, 2, 4] y2 = [0, 1, 1, 2] plt.fill_between(x, y1, y2, color='green', alpha=0.5) plt.show()
Solution
Step 1: Understand plt.fill_between with two y arrays
When two y arrays are given,plt.fill_betweenfills the area between these two curves along the x-axis.Step 2: Analyze color and alpha parameters
The color is set to green and alpha=0.5 means 50% transparency, so the filled area will appear as a translucent green band between y1 and y2.Final Answer:
A green shaded area between y1 and y2 curves with 50% transparency. -> Option DQuick Check:
Two y arrays fill area between curves [OK]
- Thinking it plots lines instead of filled area
- Assuming alpha controls line thickness
- Believing plt.fill_between cannot take two y arrays
import matplotlib.pyplot as plt x = [1, 2, 3] y = [2, 3] plt.fill_between(x, y) plt.show()
Solution
Step 1: Check array lengths for plt.fill_between
The x array has 3 elements, but y has only 2. This mismatch causes a ValueError because the function expects arrays of equal length.Step 2: Verify other requirements
Color argument is optional, and plt.plot is not required before fill_between. So options B and C are incorrect.Final Answer:
x and y arrays have different lengths causing a ValueError. -> Option CQuick Check:
Array length mismatch causes error [OK]
- Ignoring array length mismatch
- Thinking color is mandatory
- Assuming plt.plot must be called first
Solution
Step 1: Use the 'where' parameter to conditionally fill
Thewhereargument inplt.fill_betweenallows filling only where a condition is true. To fill where y1 is greater than y2, usewhere=(y1 > y2).Step 2: Check other options for correctness
plt.fill_between(x, y1, y2, color='blue', alpha=0.3) fills everywhere between y1 and y2. plt.fill_between(x, y1, y2, where=(y2 > y1), color='blue', alpha=0.3) fills where y2 is greater, which is opposite. plt.fill_between(x, y1, y2, where=(y1 == y2), color='blue', alpha=0.3) fills only where y1 equals y2, which is usually no area.Final Answer:
plt.fill_between(x, y1, y2, where=(y1 > y2), color='blue', alpha=0.3) -> Option AQuick Check:
Use where=(y1 > y2) to fill selectively [OK]
- Omitting the where parameter for conditional fill
- Reversing the condition in where
- Using equality instead of greater than
Ready to prove it?
Pass the test and this pattern moves unknown
