Bird
Raised Fist0
Matplotlibdata~20 mins

Waterfall chart pattern in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a waterfall chart in data visualization?
easy
A. To plot continuous data over time
B. To display the distribution of data points in categories
C. To show step-by-step changes from a starting value to an ending value
D. To compare multiple variables side by side

Solution

  1. Step 1: Understand waterfall chart concept

    A waterfall chart visually breaks down how an initial value changes through a series of positive and negative steps to reach a final value.
  2. Step 2: Compare with other chart types

    Other charts like histograms or line charts serve different purposes, such as distribution or trends over time, not stepwise changes.
  3. Final Answer:

    To show step-by-step changes from a starting value to an ending value -> Option C
  4. Quick Check:

    Waterfall charts = stepwise changes [OK]
Hint: Waterfall charts show how values add or subtract stepwise [OK]
Common Mistakes:
  • Confusing waterfall charts with line charts
  • Thinking waterfall charts show data distribution
  • Assuming waterfall charts compare unrelated categories
2. Which of the following is the correct way to set the bottom parameter in plt.bar() to create a waterfall effect?
easy
A. Use cumulative sums of previous bars as the bottom values
B. Set bottom to zero for all bars
C. Use random values for bottom to create spacing
D. Set bottom to the maximum value of the dataset

Solution

  1. Step 1: Understand the bottom parameter in bar charts

    The bottom parameter sets where each bar starts vertically. For waterfall charts, bars start where the previous bar ended.
  2. Step 2: Use cumulative sums for correct stacking

    By using cumulative sums of previous values, each bar stacks correctly to show stepwise changes.
  3. Final Answer:

    Use cumulative sums of previous bars as the bottom values -> Option A
  4. Quick Check:

    Bottom = cumulative sums for waterfall [OK]
Hint: Bottom bars stack by adding previous bar heights [OK]
Common Mistakes:
  • Setting bottom always to zero causes overlapping bars
  • Using random bottoms breaks the waterfall flow
  • Setting bottom to max value misplaces bars
3. What will be the output of this code snippet creating a waterfall chart?
import matplotlib.pyplot as plt
values = [100, -30, 20, -10]
cumulative = [100, 70, 90, 80]
plt.bar(range(4), values, bottom=[0, 100, 70, 90], color=['green', 'red', 'green', 'red'])
plt.show()
medium
A. A waterfall chart showing increases and decreases starting at 100
B. A bar chart with all bars starting at zero
C. A stacked bar chart with bars overlapping incorrectly
D. An error because bottom list length does not match values

Solution

  1. Step 1: Analyze the bottom parameter values

    The bottom list [0, 100, 70, 90] correctly positions each bar to start where the previous ended, matching cumulative sums.
  2. Step 2: Check colors and values

    Positive values are green, negative are red, showing increases and decreases clearly in a waterfall style.
  3. Final Answer:

    A waterfall chart showing increases and decreases starting at 100 -> Option A
  4. Quick Check:

    Bottom aligns bars for waterfall effect [OK]
Hint: Check bottom aligns with cumulative sums for correct waterfall [OK]
Common Mistakes:
  • Confusing bottom values causing bars to overlap
  • Ignoring color coding for increases/decreases
  • Assuming bars start all at zero
4. Identify the error in this waterfall chart code:
import matplotlib.pyplot as plt
values = [50, -20, 30]
cumulative = [50, 30, 60]
plt.bar(range(3), values, bottom=[0, 50, 60], color=['green', 'red', 'green'])
plt.show()
medium
A. The bottom list should be [0, 50, 80] because 30 + 50 = 80
B. The bottom list should be [0, 50, 30] to match cumulative sums
C. The color list length does not match values length
D. The values list contains negative numbers which is not allowed

Solution

  1. Step 1: Analyze cumulative sums and bottoms

    The cumulative sums are [50, 30, 60]. The bottoms are [0, 50, 60]. The third bottom should be 30 (cumulative[1]), not 60.
  2. Step 2: Identify the mismatch

    The bottom list does not correctly match the cumulative sums; it should be [0, 50, 30].
  3. Final Answer:

    The bottom list should be [0, 50, 30] to match cumulative sums -> Option B
  4. Quick Check:

    Bottoms = [0] + cumulative[:-1] [OK]
Hint: Bottom bars must start where previous bar ends [OK]
Common Mistakes:
  • Using incorrect cumulative sums for bottom
  • Mismatching color list length
  • Assuming negative values are invalid
5. You want to create a waterfall chart for monthly profit changes: start at 200, then +50, -30, +20, -10. Which code snippet correctly calculates the bottoms for the bars?
hard
A. bottoms = [0, 50, 20, -10, 0]
B. bottoms = [200, 250, 220, 240, 230]
C. bottoms = [200, 0, 0, 0, 0]
D. bottoms = [0, 200, 250, 220, 240]

Solution

  1. Step 1: Calculate cumulative sums stepwise

    Start at 200, then add +50 = 250, then -30 = 220, then +20 = 240, then -10 = 230.
  2. Step 2: Determine bottoms for each bar

    The first bar starts at 0, second at 200, third at 250, fourth at 220, fifth at 240 to show stepwise changes.
  3. Final Answer:

    bottoms = [0, 200, 250, 220, 240] -> Option D
  4. Quick Check:

    Bottoms = previous cumulative sums [OK]
Hint: Bottoms = cumulative sums before each step [OK]
Common Mistakes:
  • Starting bottoms at data values instead of zero
  • Using incorrect cumulative sums
  • Setting all bottoms to zero
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard