Bird
Raised Fist0
Matplotlibdata~10 mins

Time series with fill_between in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Time series with fill_between
Prepare time series data
↓
Plot line graph of data
↓
Use fill_between to shade area
↓
Show plot with shaded area
We first prepare time series data, then plot it as a line graph, next use fill_between to shade the area under or between lines, and finally display the plot.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
y = np.sin(x / 2)
plt.plot(x, y)
plt.fill_between(x, y, color='skyblue', alpha=0.5)
plt.show()
This code plots a sine wave over 10 points and shades the area under the curve with a light blue color.
Execution Table
StepActionVariable/FunctionResult/Effect
1Import matplotlib.pyplot and numpyplt, npModules ready for plotting and data
2Create x as array 0 to 9x = np.arange(10)x = [0 1 2 3 4 5 6 7 8 9]
3Calculate y as sine of x/2y = np.sin(x / 2)y approx [0.0, 0.479, 0.841, 0.909, 0.997, 0.598, 0.141, -0.351, -0.757, -0.978]
4Plot line graph of y vs xplt.plot(x, y)Line graph drawn on plot
5Fill area under y with color skyblue and alpha 0.5plt.fill_between(x, y, color='skyblue', alpha=0.5)Area under line shaded light blue
6Show the plotplt.show()Plot window opens with line and shaded area
💡 Plot displayed, execution ends
Variable Tracker
VariableStartAfter Step 2After Step 3Final
xundefined[0 1 2 3 4 5 6 7 8 9][0 1 2 3 4 5 6 7 8 9][0 1 2 3 4 5 6 7 8 9]
yundefinedundefined[0.0 0.47942554 0.84147098 0.90929743 0.99749499 0.59847214 0.14112001 -0.35078323 -0.7568025 -0.97753012][0.0 0.47942554 0.84147098 0.90929743 0.99749499 0.59847214 0.14112001 -0.35078323 -0.7568025 -0.97753012]
Key Moments - 3 Insights
Why does fill_between shade the area under the curve and not above?
fill_between shades the area between the x-axis (default y=0) and the y values. Since y is positive or negative, the shaded area follows the curve down to zero, as shown in step 5 of the execution_table.
What happens if y has negative values in fill_between?
fill_between shades between y and zero, so if y is negative, the shaded area appears below the x-axis line, visible in the negative y values in step 3 and shaded in step 5.
Can fill_between shade between two different lines?
Yes, by providing two y arrays to fill_between, it shades the area between them. Here, only one y is given, so it shades between y and zero (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the approximate value of y at x=3?
A0.76
B0.24
C0.99
D-0.28
💡 Hint
Check the y array values in step 3 of execution_table; y at index 3 is about 0.909
At which step does the shaded area get added to the plot?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look at the Action column in execution_table; fill_between is called at step 5
If we change fill_between to fill between y and y+1, how would the shaded area change?
AIt would shade area between y and y+1, above the curve
BIt would shade below y only
CNo shading would appear
DIt would shade between 0 and y only
💡 Hint
Recall fill_between can take two y arrays to shade between, as explained in key_moments point 3
Concept Snapshot
Time series with fill_between:
- Prepare x (time) and y (values) arrays
- Plot line graph with plt.plot(x, y)
- Use plt.fill_between(x, y, color, alpha) to shade area under curve
- Shows visually the area between curve and baseline (default y=0)
- Useful for highlighting ranges or confidence intervals
Full Transcript
This visual execution traces how to plot a time series line graph and shade the area under it using matplotlib's fill_between. First, we import necessary modules. Then, we create x as a sequence of numbers and y as sine values of x divided by 2. We plot y against x as a line. Next, fill_between shades the area between the curve and the x-axis with a light blue color. Finally, the plot is displayed. Key points include understanding that fill_between shades between two y-values (default zero and y), so negative y values shade below the axis. We also see how variables x and y change step-by-step. The quiz checks understanding of y values, when shading happens, and how changing fill_between parameters affects shading.

Practice

(1/5)
1. What does the fill_between function do in a time series plot using matplotlib?
easy
A. Creates a bar chart from time series data
B. Plots scatter points on the time series
C. Colors the area between two lines to highlight differences or ranges
D. Removes grid lines from the plot

Solution

  1. Step 1: Understand the purpose of fill_between

    The fill_between function is used to color the area between two lines or curves in a plot.
  2. Step 2: Connect to time series visualization

    In time series plots, it highlights differences or ranges between two data series by filling the area between them.
  3. Final Answer:

    Colors the area between two lines to highlight differences or ranges -> Option C
  4. Quick Check:

    fill_between colors area between lines = A [OK]
Hint: Remember fill_between fills area between lines visually [OK]
Common Mistakes:
  • Confusing fill_between with scatter plot
  • Thinking it creates bar charts
  • Assuming it removes grid lines
2. Which of the following is the correct syntax to fill the area between two time series arrays y1 and y2 over time points x using matplotlib?
easy
A. plt.fill_between(x, y1, y2, color='blue', alpha=0.3)
B. plt.fill_between(x, y1, color='blue', alpha=0.3)
C. plt.fill_between(y1, y2, x, color='blue')
D. plt.fill_between(x, y1, y2, linestyle='--')

Solution

  1. Step 1: Check function parameters order

    The correct order for fill_between is x (time points), then y1 and y2 (values to fill between).
  2. Step 2: Validate optional arguments

    Color and transparency (alpha) are valid optional arguments; linestyle is not used for fill_between.
  3. Final Answer:

    plt.fill_between(x, y1, y2, color='blue', alpha=0.3) -> Option A
  4. Quick Check:

    fill_between(x, y1, y2) with color and alpha = C [OK]
Hint: Remember order: x, y1, y2 in fill_between [OK]
Common Mistakes:
  • Swapping y1 and y2 with x
  • Using linestyle instead of color or alpha
  • Omitting one of the y arrays
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y1 = np.array([1, 3, 2, 5, 4])
y2 = np.array([2, 4, 3, 6, 5])
plt.plot(x, y1, label='y1')
plt.plot(x, y2, label='y2')
plt.fill_between(x, y1, y2, color='green', alpha=0.4)
plt.legend()
plt.show()
medium
A. An error because fill_between requires y1 > y2
B. A plot with two lines but no shaded area
C. A plot with only one line and a green shaded area below it
D. A plot with two lines and a green shaded area between them

Solution

  1. Step 1: Understand plot and fill_between behavior

    The code plots two lines y1 and y2 over x, then fills the area between them with green color and transparency.
  2. Step 2: Confirm fill_between works with any order

    fill_between fills area between y1 and y2 regardless of which is greater, so no error occurs.
  3. Final Answer:

    A plot with two lines and a green shaded area between them -> Option D
  4. Quick Check:

    Two lines + green fill_between area = A [OK]
Hint: fill_between shades area between lines regardless of order [OK]
Common Mistakes:
  • Thinking fill_between needs y1 > y2 always
  • Expecting no shaded area without fill_between call
  • Confusing plot lines with fill_between shading
4. Identify the error in this code snippet that tries to fill between two time series:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y1 = [1, 2, 3]
y2 = [2, 3, 4, 5]
plt.fill_between(x, y1, y2, color='red')
plt.show()
medium
A. x and y1 have different lengths causing a ValueError
B. fill_between does not accept list inputs
C. Missing plt.plot calls before fill_between
D. Color argument must be uppercase

Solution

  1. Step 1: Check lengths of x, y1, and y2

    x has 4 points, y1 has 3 points, y2 has 4 points; mismatch causes error.
  2. Step 2: Understand fill_between input requirements

    fill_between requires x, y1, y2 arrays to have the same length to plot correctly.
  3. Final Answer:

    x and y1 have different lengths causing a ValueError -> Option A
  4. Quick Check:

    Length mismatch in inputs = B [OK]
Hint: Check all input arrays have same length before fill_between [OK]
Common Mistakes:
  • Ignoring length mismatch errors
  • Thinking fill_between needs plot calls first
  • Assuming color argument case matters
5. You have two time series arrays y1 and y2 representing daily temperatures for two cities over 7 days. You want to highlight only the days when y1 is higher than y2 using fill_between. Which code snippet correctly achieves this?
hard
A. plt.fill_between(x, y1, y2, condition=(y1 > y2), color='orange')
B. plt.fill_between(x, y1, y2, where=(y1 > y2), color='orange', alpha=0.5)
C. plt.fill_between(x, y1, y2, where=(y1 < y2), color='orange', alpha=0.5)
D. plt.fill_between(x, y1, y2, color='orange', alpha=0.5)

Solution

  1. Step 1: Use the 'where' parameter to specify condition

    The where argument in fill_between allows filling only where the condition is True.
  2. Step 2: Set condition to y1 > y2 and specify color and transparency

    Using where=(y1 > y2) fills only days when y1 is higher than y2 with orange color and alpha 0.5.
  3. Final Answer:

    plt.fill_between(x, y1, y2, where=(y1 > y2), color='orange', alpha=0.5) -> Option B
  4. Quick Check:

    Use where=(y1 > y2) to fill selectively = D [OK]
Hint: Use 'where' to fill conditionally between lines [OK]
Common Mistakes:
  • Using 'condition' instead of 'where'
  • Filling without condition fills entire area
  • Reversing the condition (y1 < y2) by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard