Bird
Raised Fist0
Matplotlibdata~20 mins

Stacked area chart in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stacked Area Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a basic stacked area chart code
What will be the output of this code snippet that creates a stacked area chart using matplotlib?
Matplotlib
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C']
data = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
plt.stackplot(range(3), *data, labels=labels)
plt.legend(loc='upper left')
plt.show()
AA line chart with three lines labeled A, B, and C
BA stacked area chart with three layers increasing from left to right, labeled A, B, and C
CA bar chart with three bars labeled A, B, and C
DA scatter plot with points labeled A, B, and C
Attempts:
2 left
💡 Hint
Stacked area charts fill areas between lines stacked on top of each other.
❓ data_output
intermediate
1:30remaining
Number of layers in stacked area chart
Given the data below used in a stacked area chart, how many layers will the chart have?
Matplotlib
data = [[5, 3, 6, 2], [1, 4, 2, 3], [3, 2, 1, 5], [4, 1, 3, 4]]
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Each inner list represents one layer in the stacked area chart.
🔧 Debug
advanced
2:00remaining
Identify the error in stacked area chart code
What error will this code raise when trying to plot a stacked area chart?
Matplotlib
import matplotlib.pyplot as plt

x = [0, 1, 2]
y = [[1, 2], [3, 4], [5, 6]]
plt.stackplot(x, *y)
plt.show()
AValueError: x and y must have same length
BIndexError: list index out of range
CTypeError: unhashable type: 'list'
DNo error, chart displays correctly
Attempts:
2 left
💡 Hint
Check if x and y data lengths match for plotting.
❓ visualization
advanced
1:30remaining
Effect of alpha parameter in stacked area chart
What visual effect does setting alpha=0.5 have on a stacked area chart in matplotlib?
Matplotlib
import matplotlib.pyplot as plt

x = [0, 1, 2, 3]
y = [[1, 2, 3, 4], [2, 1, 2, 1]]
plt.stackplot(x, *y, alpha=0.5)
plt.show()
AThe stacked areas become semi-transparent, allowing background or overlapping colors to show through
BThe stacked areas become fully opaque and brighter
CThe stacked areas disappear completely
DThe stacked areas change to dashed outlines without fill
Attempts:
2 left
💡 Hint
Alpha controls transparency in matplotlib plots.
🧠 Conceptual
expert
2:30remaining
Interpreting stacked area chart data trends
In a stacked area chart showing sales of three products over time, if the total height of the stack increases but the bottom layer's area stays the same, what does this indicate?
ASales of the bottom product increased and others decreased
BSales of all products increased equally
CSales of the bottom product decreased while others increased
DSales of the bottom product stayed constant while sales of other products increased
Attempts:
2 left
💡 Hint
The bottom layer's area represents its absolute value over time.

Practice

(1/5)
1. What does a stacked area chart in matplotlib primarily show?
easy
A. How different parts add up to a whole over categories or time
B. Only the total sum of all data points
C. The relationship between two unrelated variables
D. The frequency distribution of a single variable

Solution

  1. Step 1: Understand the purpose of stacked area charts

    Stacked area charts display multiple data series stacked on top of each other to show how each part contributes to the total over time or categories.
  2. Step 2: Compare options with this purpose

    How different parts add up to a whole over categories or time correctly describes this purpose. Options A, B, and C describe other chart types or incorrect uses.
  3. Final Answer:

    How different parts add up to a whole over categories or time -> Option A
  4. Quick Check:

    Stacked area chart = parts adding to whole [OK]
Hint: Stacked area charts show parts stacked to form a whole [OK]
Common Mistakes:
  • Confusing stacked area chart with line chart
  • Thinking it shows only totals, not parts
  • Mixing it up with scatter plots
2. Which of the following is the correct way to create a stacked area chart using matplotlib.pyplot?
easy
A. plt.stackplot(x, y1, y2, labels=['A', 'B'])
B. plt.area(x, y1, y2)
C. plt.plot_area(x, y1, y2)
D. plt.stack(x, y1, y2)

Solution

  1. Step 1: Recall the correct function name

    The correct function to create stacked area charts in matplotlib is stackplot from matplotlib.pyplot.
  2. Step 2: Check the syntax

    plt.stackplot(x, y1, y2, labels=['A', 'B']) uses plt.stackplot(x, y1, y2, labels=['A', 'B']), which is correct. Other options use non-existent functions.
  3. Final Answer:

    plt.stackplot(x, y1, y2, labels=['A', 'B']) -> Option A
  4. Quick Check:

    Function name is stackplot [OK]
Hint: Use plt.stackplot for stacked area charts [OK]
Common Mistakes:
  • Using plt.area which does not exist
  • Trying plt.plot_area instead of stackplot
  • Forgetting to pass x and multiple y arrays
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [1, 2, 3]
y2 = [2, 1, 2]
plt.stackplot(x, y1, y2, labels=['A', 'B'])
plt.legend()
plt.show()
medium
A. An error because y arrays have different lengths
B. A line chart with two lines labeled 'A' and 'B'
C. A bar chart with two bars for each x value
D. A stacked area chart with two layers labeled 'A' and 'B' over x=1 to 3

Solution

  1. Step 1: Analyze the data lengths and function

    Both y1 and y2 have length 3 matching x. The function plt.stackplot creates a stacked area chart.
  2. Step 2: Understand the output

    The code plots two stacked areas labeled 'A' and 'B' over x values 1 to 3, then shows a legend.
  3. Final Answer:

    A stacked area chart with two layers labeled 'A' and 'B' over x=1 to 3 -> Option D
  4. Quick Check:

    Stackplot with matching lengths = stacked area chart [OK]
Hint: Check lengths of x and y arrays match for stackplot [OK]
Common Mistakes:
  • Thinking stackplot creates line or bar charts
  • Assuming error due to lengths when they match
  • Ignoring legend labels
4. Identify the error in this code that tries to plot a stacked area chart:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [1, 2]
y2 = [2, 1, 2]
plt.stackplot(x, y1, y2)
plt.show()
medium
A. Missing labels argument causes the plot to fail
B. plt.stackplot does not accept multiple y arrays
C. x and y1 have different lengths causing a ValueError
D. plt.show() is missing parentheses

Solution

  1. Step 1: Check lengths of x and y arrays

    x has length 3, y1 has length 2, y2 has length 3. Length mismatch causes error.
  2. Step 2: Understand stackplot requirements

    All y arrays must have the same length as x. This mismatch causes a ValueError.
  3. Final Answer:

    x and y1 have different lengths causing a ValueError -> Option C
  4. Quick Check:

    Length mismatch in stackplot = error [OK]
Hint: Ensure all y arrays match x length in stackplot [OK]
Common Mistakes:
  • Assuming labels are required to avoid errors
  • Thinking stackplot accepts different length arrays
  • Forgetting to call plt.show() with parentheses
5. You have monthly sales data for three products over 6 months:
months = [1, 2, 3, 4, 5, 6]
product_a = [5, 7, 8, 6, 7, 9]
product_b = [3, 4, 2, 5, 6, 4]
product_c = [2, 3, 4, 3, 2, 3]

How would you create a stacked area chart showing each product's contribution over time with a legend?
hard
A. plt.plot(months, product_a + product_b + product_c) plt.legend(['Total']) plt.show()
B. plt.stackplot(months, product_a, product_b, product_c, labels=['A', 'B', 'C']) plt.legend() plt.show()
C. plt.bar(months, product_a, bottom=product_b) plt.show()
D. plt.stackplot(product_a, product_b, product_c, labels=['A', 'B', 'C']) plt.legend() plt.show()

Solution

  1. Step 1: Use plt.stackplot with x and multiple y arrays

    Pass months as x and product sales as y arrays with labels for legend.
  2. Step 2: Add legend and show plot

    Call plt.legend() to show labels and plt.show() to display the chart.
  3. Final Answer:

    plt.stackplot(months, product_a, product_b, product_c, labels=['A', 'B', 'C']) plt.legend() plt.show() -> Option B
  4. Quick Check:

    Stackplot with labels + legend = stacked area chart [OK]
Hint: Pass x, multiple y arrays, labels, then call plt.legend() [OK]
Common Mistakes:
  • Adding y arrays instead of stacking
  • Using bar chart with wrong bottom parameter
  • Forgetting to pass x as first argument
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