0
0
Matplotlibdata~20 mins

Waterfall chart pattern in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Waterfall Chart Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Waterfall Chart Data Calculation

What is the final cumulative value after applying the waterfall steps in this code?

Matplotlib
steps = [100, -30, 20, -10]
cumulative = 0
for step in steps:
    cumulative += step
print(cumulative)
A90
B100
C120
D80
Attempts:
2 left
💡 Hint

Add each step value one by one to get the final total.

data_output
intermediate
2:30remaining
Resulting DataFrame for Waterfall Chart

Given the following code to prepare data for a waterfall chart, what is the resulting DataFrame?

Matplotlib
import pandas as pd

steps = ['Start', 'Sales', 'Returns', 'Marketing', 'End']
values = [1000, 300, -50, -100, 0]
cumulative = 0
cumulative_values = []
for v in values:
    cumulative += v
    cumulative_values.append(cumulative)
data = pd.DataFrame({'Step': steps, 'Value': values, 'Cumulative': cumulative_values})
print(data)
A
Step      Value  Cumulative
0     Start   1000        1000
1     Sales    300        1300
2   Returns    -50        1250
3  Marketing  -100        1150
4       End      0        1150
B
Step      Value  Cumulative
0     Start   1000        1000
1     Sales    300         300
2   Returns    -50         50
3  Marketing  -100        -50
4       End      0          0
C
Step      Value  Cumulative
0     Start   1000        1000
1     Sales    300        1300
2   Returns    -50         50
3  Marketing  -100        -50
4       End      0          0
D
Step      Value  Cumulative
0     Start   1000        1000
1     Sales    300        1300
2   Returns    -50        1250
3  Marketing  -100        1150
4       End      0          0
Attempts:
2 left
💡 Hint

Cumulative sums add each value step by step.

visualization
advanced
3:00remaining
Identify the Correct Waterfall Chart Plot

Which option shows the correct matplotlib code to plot a waterfall chart with positive and negative steps?

A
import matplotlib.pyplot as plt
steps = ['Start', 'Profit', 'Loss', 'End']
values = [100, 50, -30, 120]

fig, ax = plt.subplots()
cumulative = 0
for i, v in enumerate(values):
    ax.bar(i, abs(v), bottom=cumulative, color='green' if v >= 0 else 'red')
    cumulative += v
ax.set_xticks(range(len(steps)))
ax.set_xticklabels(steps)
plt.show()
B
import matplotlib.pyplot as plt
steps = ['Start', 'Profit', 'Loss', 'End']
values = [100, 50, -30, 120]

fig, ax = plt.subplots()
cumulative = 0
for i, v in enumerate(values):
    ax.bar(i, v, bottom=cumulative, color='green' if v >= 0 else 'red')
    cumulative += v
ax.set_xticks(range(len(steps)))
ax.set_xticklabels(steps)
plt.show()
C
import matplotlib.pyplot as plt
steps = ['Start', 'Profit', 'Loss', 'End']
values = [100, 50, -30, 120]

fig, ax = plt.subplots()
cumulative = 0
for i, v in enumerate(values):
    ax.bar(i, cumulative, bottom=v, color='green' if v >= 0 else 'red')
    cumulative += v
ax.set_xticks(range(len(steps)))
ax.set_xticklabels(steps)
plt.show()
D
import matplotlib.pyplot as plt
steps = ['Start', 'Profit', 'Loss', 'End']
values = [100, 50, -30, 120]

fig, ax = plt.subplots()
cumulative = 0
for i, v in enumerate(values):
    ax.bar(i, v, color='green' if v >= 0 else 'red')
    cumulative += v
ax.set_xticks(range(len(steps)))
ax.set_xticklabels(steps)
plt.show()
Attempts:
2 left
💡 Hint

Waterfall bars start from the cumulative total so far.

🔧 Debug
advanced
2:00remaining
Identify the Error in Waterfall Chart Data Preparation

What error will this code raise when preparing data for a waterfall chart?

Matplotlib
values = [100, -20, 30]
cumulative = 0
cumulative_values = []
for v in values:
    cumulative_values.append(cumulative + v)
    cumulative = v
print(cumulative_values)
ANo error, output is [100, 80, 10]
BNo error, output is [100, -20, 30]
CNo error, output is [100, 120, 150]
DNo error, output is [100, 80, 30]
Attempts:
2 left
💡 Hint

Check how cumulative is updated inside the loop.

🚀 Application
expert
1:30remaining
Calculate Number of Bars in a Waterfall Chart

You have a dataset with 5 categories and 1 total. You want to create a waterfall chart showing each category's contribution and the final total. How many bars will the chart have?

A5 bars
B7 bars
C6 bars
D4 bars
Attempts:
2 left
💡 Hint

Count each category plus the total bar.