0
0
Matplotlibdata~10 mins

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

Choose your learning style9 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.