0
0
Matplotlibdata~10 mins

Waterfall chart pattern in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Waterfall chart pattern
Start with initial value
Add positive changes
Subtract negative changes
Calculate intermediate totals
Plot bars step-by-step
Show final total bar
End
A waterfall chart starts with an initial value, then adds or subtracts changes step-by-step, showing intermediate totals and a final total bar.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
values = [100, -20, 30, -10, 40]
labels = ['Start', 'Loss', 'Gain', 'Loss', 'Gain']
plt.bar(range(len(values)), values)
plt.show()
This code plots simple bars for each value, but does not yet show the waterfall pattern with cumulative totals.
Execution Table
StepIndexValueRunning TotalBar StartBar HeightBar Color
101001000100blue
21-20808020red
32301108030blue
43-1010011010red
544014010040blue
6Total1400140green
💡 All values processed, final total bar shows cumulative sum 140
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
running_total010080110100140140
bar_start0080801101000
bar_height10020301040140
bar_colorblueredblueredbluegreen
Key Moments - 3 Insights
Why does the bar start position change for negative values?
Negative values are drawn downward from the current running total, so the bar start is the running total before this step (see execution_table rows 2 and 4).
How is the final total bar different from other bars?
The final total bar starts at zero and its height is the cumulative sum of all values, shown in execution_table row 6 with color green.
Why do some bars have red color and others blue?
Positive values use blue bars to show increases, negative values use red bars to show decreases, as tracked in variable_tracker 'bar_color'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the running total after step 3?
A80
B110
C100
D140
💡 Hint
Check the 'Running Total' column at step 3 in the execution_table.
At which step does the bar color change to red for the first time?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Bar Color' column in execution_table rows.
If the value at index 1 changed from -20 to -50, what would be the new running total after step 2?
A50
B80
C30
D100
💡 Hint
Subtract 50 from the initial 100 to find the new running total after step 2.
Concept Snapshot
Waterfall chart pattern:
- Start with initial value
- Add/subtract changes stepwise
- Bars start at previous total
- Positive bars in one color, negative in another
- Final bar shows total sum
Full Transcript
A waterfall chart visually shows how an initial value changes step-by-step by adding or subtracting amounts. Each bar starts where the previous bar ended, with positive changes going up and negative changes going down. The final bar shows the total after all changes. This pattern helps understand how individual parts affect the whole.