0
0
Matplotlibdata~10 mins

Area chart with plt.fill_between in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Area chart with plt.fill_between
Prepare x and y data
Call plt.fill_between(x, y1, y2)
Matplotlib fills area between y1 and y2
Display or save the plot
This flow shows how data arrays are prepared, then plt.fill_between fills the area between two y-values over x, and finally the plot is shown.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [1, 3, 2, 5, 4]
plt.fill_between(x, y1, color='skyblue')
plt.show()
This code draws an area chart by filling the area under y1 over x with a sky blue color.
Execution Table
StepActionx valuesy1 valuesfill_between effectPlot state
1Prepare x and y1 data[1, 2, 3, 4, 5][1, 3, 2, 5, 4]No fill yetEmpty plot
2Call plt.fill_between(x, y1)[1, 2, 3, 4, 5][1, 3, 2, 5, 4]Area under y1 filled with skybluePlot has filled area
3Call plt.show()[1, 2, 3, 4, 5][1, 3, 2, 5, 4]Area displayed on screenPlot visible to user
💡 Plot is displayed and script ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
xundefined[1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5]
y1undefined[1, 3, 2, 5, 4][1, 3, 2, 5, 4][1, 3, 2, 5, 4]
plot stateemptyemptyarea filleddisplayed
Key Moments - 2 Insights
Why does plt.fill_between fill the area under the curve and not above?
By default, plt.fill_between fills the area between y1 and y2=0 (the x-axis). Since y2 is not given, it assumes zero, so it fills under y1 as shown in step 2 of the execution_table.
What happens if x and y1 have different lengths?
Matplotlib will raise an error because x and y1 must be the same length to match points. This is why in step 1 both arrays have length 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what color is used to fill the area?
Agreen
Bred
Cskyblue
Dno color
💡 Hint
Check the 'fill_between effect' column in step 2 of execution_table.
At which step does the plot become visible to the user?
AStep 1
BStep 3
CStep 2
DAfter script ends
💡 Hint
Look at the 'Plot state' column in execution_table.
If y1 was all zeros, what would plt.fill_between show?
AA filled area at zero height (flat line)
BNo filled area
CAn error
DA filled area above the x-axis
💡 Hint
Think about how fill_between fills area between y1 and zero baseline.
Concept Snapshot
plt.fill_between(x, y1, y2=0, color) fills the area between y1 and y2 over x.
If y2 is omitted, it defaults to zero (x-axis).
Use it to create area charts showing magnitude under curves.
Call plt.show() to display the plot.
x and y arrays must be same length.
Full Transcript
This visual execution shows how to create an area chart using matplotlib's plt.fill_between function. First, we prepare the x and y data arrays. Then, calling plt.fill_between fills the area between y1 and the x-axis with a chosen color. Finally, plt.show displays the plot on screen. The execution table traces these steps, showing variable values and plot state changes. Key moments clarify why the area fills under the curve and the importance of matching array lengths. The quiz tests understanding of color, display timing, and behavior with zero values.