0
0
Matplotlibdata~5 mins

Waterfall chart pattern in Matplotlib

Choose your learning style9 modes available
Introduction

A waterfall chart helps you see how a starting value changes step-by-step to reach a final value. It shows increases and decreases clearly.

To show how monthly sales add up to a yearly total.
To explain how different expenses reduce a budget.
To visualize profit changes from revenue to net income.
To track changes in inventory levels over time.
To display stepwise changes in project costs.
Syntax
Matplotlib
import matplotlib.pyplot as plt

# Define data
labels = ['Start', 'Increase', 'Decrease', 'End']
values = [100, 30, -20, 110]

# Calculate cumulative sums for bar positions
cumulative = [0]
for v in values[:-1]:
    cumulative.append(cumulative[-1] + v)

# Plot bars
for i in range(len(values)):
    plt.bar(labels[i], values[i], bottom=cumulative[i])

plt.title('Waterfall Chart')
plt.show()

You create a waterfall chart by stacking bars on top of each other.

The 'bottom' parameter in plt.bar sets where each bar starts vertically.

Examples
Simple data setup with positive and negative changes.
Matplotlib
labels = ['Start', 'Gain', 'Loss', 'End']
values = [200, 50, -30, 220]
Calculate where each bar should start to stack properly.
Matplotlib
cumulative = [0]
for v in values[:-1]:
    cumulative.append(cumulative[-1] + v)
Draw each bar at the correct position to form the waterfall.
Matplotlib
for i in range(len(values)):
    plt.bar(labels[i], values[i], bottom=cumulative[i])
Sample Program

This program shows a waterfall chart with positive and negative changes. Blue bars go up, red bars go down.

Matplotlib
import matplotlib.pyplot as plt

labels = ['Start', 'Sales', 'Returns', 'Marketing', 'End']
values = [1000, 300, -150, -200, 950]

cumulative = [0]
for v in values[:-1]:
    cumulative.append(cumulative[-1] + v)

colors = ['blue' if v >= 0 else 'red' for v in values]

for i in range(len(values)):
    plt.bar(labels[i], values[i], bottom=cumulative[i], color=colors[i])

plt.title('Waterfall Chart Example')
plt.ylabel('Amount')
plt.show()
OutputSuccess
Important Notes

Waterfall charts are not built-in in matplotlib, so you build them by stacking bars.

Use colors to show increases (e.g., blue) and decreases (e.g., red) clearly.

Label bars clearly so viewers understand each step.

Summary

Waterfall charts show step-by-step changes from a start to an end value.

They are made by stacking bars using the 'bottom' parameter in matplotlib.

Use colors and labels to make the chart easy to read.