0
0
Matplotlibdata~10 mins

Stacked area chart in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stacked area chart
Prepare data series
Calculate cumulative sums
Plot areas stacked by cumulative values
Add labels and legend
Display chart
We start with data series, compute cumulative sums to stack them, then plot each area on top of the previous, add labels, and show the chart.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y1 = [1, 3, 2, 4]
y2 = [2, 1, 3, 1]

plt.stackplot(x, y1, y2, labels=['A', 'B'])
plt.legend()
plt.show()
This code creates a stacked area chart with two data series labeled A and B over x values.
Execution Table
StepActionData StatePlot StateOutput
1Prepare x and y datax=[1,2,3,4], y1=[1,3,2,4], y2=[2,1,3,1]No plot yetNo output
2Calculate cumulative sums for stackingCum y1 = y1, Cum y2 = y1 + y2 = [3,4,5,5]Ready to plot stacked areasNo output
3Plot first area y1N/AArea for y1 plotted from 0 to y1Partial stacked area chart
4Plot second area y2N/AArea for y2 plotted from y1 to y1+y2Stacked area chart with two layers
5Add legend with labels 'A' and 'B'N/ALegend addedStacked area chart with legend
6Display chartN/AChart rendered on screenVisible stacked area chart
💡 All data series plotted and chart displayed
Variable Tracker
VariableStartAfter Step 2After Step 4Final
x[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
y1[1, 3, 2, 4][1, 3, 2, 4][1, 3, 2, 4][1, 3, 2, 4]
y2[2, 1, 3, 1][2, 1, 3, 1][2, 1, 3, 1][2, 1, 3, 1]
cum_y1N/A[1, 3, 2, 4][1, 3, 2, 4][1, 3, 2, 4]
cum_y2N/A[3, 4, 5, 5][3, 4, 5, 5][3, 4, 5, 5]
Key Moments - 3 Insights
Why do we add the second data series to the first when plotting?
Because stacking means each area starts where the previous one ended, so we add y2 values to y1 to get the top boundary of the second area, as shown in execution_table step 2.
What happens if the x values are not sorted?
The areas may look incorrect or jumbled because the plot assumes x is ordered. The code uses x=[1,2,3,4] sorted, ensuring smooth stacking as in variable_tracker.
Why do we use plt.stackplot instead of plt.plot?
plt.stackplot automatically stacks multiple data series as filled areas, while plt.plot draws lines. This is why in execution_table step 3 and 4, areas are filled cumulatively.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what is the cumulative sum of y2 at x=3?
A2
B3
C5
D4
💡 Hint
Check the 'Data State' column in step 2 for cum_y2 values.
At which step is the legend added to the chart?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the 'Action' column describing adding legend.
If y2 values were all zeros, how would the stacked area chart change?
ABoth areas would be visible but overlapping
BOnly one area for y1 would be visible
CChart would be empty
DAreas would be plotted but not stacked
💡 Hint
Think about stacking when second series is zero, referencing variable_tracker y2 values.
Concept Snapshot
Stacked area chart:
- Use plt.stackplot(x, y1, y2, ..., labels=...)
- Each area stacks on top of previous by cumulative sum
- x must be sorted for correct plotting
- Add plt.legend() for labels
- Call plt.show() to display
Full Transcript
A stacked area chart shows multiple data series stacked on top of each other as filled areas. We start by preparing x and y data. Then we calculate cumulative sums of y series to stack them. Using matplotlib's plt.stackplot, we plot each area from zero or the previous cumulative sum. We add labels with plt.legend and finally display the chart with plt.show. This visual trace shows each step from data preparation to final chart rendering.