Bird
Raised Fist0
Matplotlibdata~20 mins

Area chart with plt.fill_between in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Area Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of plt.fill_between with simple data
What will be the output of this code snippet that uses plt.fill_between to create an area chart?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 3, 5, 7]
plt.fill_between(x, y, color='skyblue')
plt.show()
AAn error because plt.fill_between requires three arguments
BA line plot connecting points (1,2), (2,3), (3,5), (4,7) without any shaded area
CA blue shaded area under the curve connecting points (1,2), (2,3), (3,5), (4,7)
DA scatter plot of points (1,2), (2,3), (3,5), (4,7) with blue dots
Attempts:
2 left
💡 Hint
Remember, plt.fill_between fills the area between the x-axis and the y-values.
🧠 Conceptual
intermediate
1:30remaining
Understanding the role of y2 in plt.fill_between
In plt.fill_between(x, y1, y2), what does the parameter y2 represent?
AThe lower boundary of the filled area
BThe upper boundary of the filled area
CThe color of the filled area
DThe x-axis values
Attempts:
2 left
💡 Hint
Think about the area being filled between two curves.
🔧 Debug
advanced
2:30remaining
Identify the error in this plt.fill_between usage
What error will this code produce and why?
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y1 = [1, 2, 3]
y2 = [0, 1, 1, 2]
plt.fill_between(x, y1, y2)
plt.show()
AIndexError because x is shorter than y2
BTypeError because y1 and y2 must be numpy arrays
CNo error, the plot will display correctly
DValueError due to mismatched lengths of x and y1
Attempts:
2 left
💡 Hint
Check if all input lists have the same length.
❓ data_output
advanced
2:00remaining
Resulting data from plt.fill_between with condition
Given this code, what is the number of points where the area is filled?
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(5)
y1 = np.array([1, 3, 2, 5, 4])
y2 = np.array([0, 2, 1, 3, 2])
plt.fill_between(x, y1, y2, where=(y1 > y2))
plt.show()
A0 points because y1 is never greater than y2
B5 points where y1 is greater than y2
C2 points where y1 is less than y2
D3 points where y1 is greater than y2
Attempts:
2 left
💡 Hint
Compare y1 and y2 values element-wise.
🚀 Application
expert
3:00remaining
Visualizing overlapping areas with plt.fill_between
You want to visualize two overlapping datasets using plt.fill_between. Which code snippet correctly fills the area between two curves y1 and y2 with different colors for the overlapping and non-overlapping regions?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y1 = np.sin(x) + 1
y2 = 0.5 * np.cos(x) + 1

# Which option correctly colors overlapping and non-overlapping areas?
A
plt.fill_between(x, y1, y2, where=(y1 >= y2), color='blue', alpha=0.5)
plt.fill_between(x, y1, y2, where=(y1 < y2), color='red', alpha=0.5)
plt.show()
B
plt.fill_between(x, y1, y2, color='blue', alpha=0.5)
plt.fill_between(x, y2, y1, color='red', alpha=0.5)
plt.show()
C
plt.fill_between(x, y1, y2, where=(y1 > y2), color='red')
plt.fill_between(x, y1, y2, where=(y1 <= y2), color='blue')
plt.show()
D
plt.fill_between(x, y1, y2, color='green')
plt.fill_between(x, y2, y1, color='yellow')
plt.show()
Attempts:
2 left
💡 Hint
Use the 'where' parameter to separate regions based on condition.

Practice

(1/5)
1. What does the plt.fill_between function do in matplotlib?
easy
A. It fills the area between two curves or a curve and a baseline.
B. It creates a scatter plot with filled markers.
C. It draws a bar chart with filled bars.
D. It adds grid lines to the plot.

Solution

  1. Step 1: Understand the purpose of plt.fill_between

    The function plt.fill_between is designed to fill the area between two lines or between a line and a baseline, creating an area chart effect.
  2. Step 2: Compare with other plot types

    Scatter plots, bar charts, and grid lines are created by different functions like plt.scatter, plt.bar, and plt.grid, not plt.fill_between.
  3. Final Answer:

    It fills the area between two curves or a curve and a baseline. -> Option A
  4. Quick Check:

    plt.fill_between fills area under curve [OK]
Hint: Remember: fill_between fills area under or between lines [OK]
Common Mistakes:
  • Confusing fill_between with scatter or bar plot functions
  • Thinking it adds grid lines
  • Assuming it only fills under a single line without baseline
2. Which of the following is the correct syntax to fill the area between x and y values using matplotlib?
easy
A. plt.fill_between(y, x)
B. plt.fill_between(x, y, color='blue')
C. plt.fill(x, y)
D. plt.fill_between(x)

Solution

  1. Step 1: Check the required parameters of plt.fill_between

    The function requires at least two arrays: x (horizontal axis) and y (vertical values). plt.fill_between(x, y, color='blue') correctly uses plt.fill_between(x, y, color='blue') which is valid syntax with an optional color argument.
  2. Step 2: Identify incorrect options

    plt.fill(x, y) uses the incorrect function plt.fill instead of plt.fill_between. plt.fill_between(y, x) reverses the x and y parameters, which is incorrect. plt.fill_between(x) misses the y values, causing an error.
  3. Final Answer:

    plt.fill_between(x, y, color='blue') -> Option B
  4. Quick Check:

    Correct syntax includes x, y, and optional color [OK]
Hint: Syntax: plt.fill_between(x, y, color='color') [OK]
Common Mistakes:
  • Using plt.fill instead of plt.fill_between
  • Swapping x and y parameters
  • Omitting y values
3. What will the following code produce?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [1, 3, 2, 4]
y2 = [0, 1, 1, 2]
plt.fill_between(x, y1, y2, color='green', alpha=0.5)
plt.show()
medium
A. A scatter plot of points from y1 and y2.
B. A green line plot of y1 with alpha transparency.
C. An error because plt.fill_between cannot take two y arrays.
D. A green shaded area between y1 and y2 curves with 50% transparency.

Solution

  1. Step 1: Understand plt.fill_between with two y arrays

    When two y arrays are given, plt.fill_between fills the area between these two curves along the x-axis.
  2. Step 2: Analyze color and alpha parameters

    The color is set to green and alpha=0.5 means 50% transparency, so the filled area will appear as a translucent green band between y1 and y2.
  3. Final Answer:

    A green shaded area between y1 and y2 curves with 50% transparency. -> Option D
  4. Quick Check:

    Two y arrays fill area between curves [OK]
Hint: Two y arrays fill area between curves with color and alpha [OK]
Common Mistakes:
  • Thinking it plots lines instead of filled area
  • Assuming alpha controls line thickness
  • Believing plt.fill_between cannot take two y arrays
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [2, 3]
plt.fill_between(x, y)
plt.show()
medium
A. plt.fill_between cannot be used without plt.plot first.
B. plt.fill_between requires a color argument.
C. x and y arrays have different lengths causing a ValueError.
D. No error; the code runs fine.

Solution

  1. Step 1: Check array lengths for plt.fill_between

    The x array has 3 elements, but y has only 2. This mismatch causes a ValueError because the function expects arrays of equal length.
  2. Step 2: Verify other requirements

    Color argument is optional, and plt.plot is not required before fill_between. So options B and C are incorrect.
  3. Final Answer:

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

    Array length mismatch causes error [OK]
Hint: Ensure x and y arrays have same length [OK]
Common Mistakes:
  • Ignoring array length mismatch
  • Thinking color is mandatory
  • Assuming plt.plot must be called first
5. You want to create an area chart showing the difference between two datasets y1 and y2 over x, but only fill where y1 is greater than y2. Which code snippet correctly achieves this?
hard
A. plt.fill_between(x, y1, y2, where=(y1 > y2), color='blue', alpha=0.3)
B. plt.fill_between(x, y1, y2, color='blue', alpha=0.3)
C. plt.fill_between(x, y1, y2, where=(y2 > y1), color='blue', alpha=0.3)
D. plt.fill_between(x, y1, y2, where=(y1 == y2), color='blue', alpha=0.3)

Solution

  1. Step 1: Use the 'where' parameter to conditionally fill

    The where argument in plt.fill_between allows filling only where a condition is true. To fill where y1 is greater than y2, use where=(y1 > y2).
  2. Step 2: Check other options for correctness

    plt.fill_between(x, y1, y2, color='blue', alpha=0.3) fills everywhere between y1 and y2. plt.fill_between(x, y1, y2, where=(y2 > y1), color='blue', alpha=0.3) fills where y2 is greater, which is opposite. plt.fill_between(x, y1, y2, where=(y1 == y2), color='blue', alpha=0.3) fills only where y1 equals y2, which is usually no area.
  3. Final Answer:

    plt.fill_between(x, y1, y2, where=(y1 > y2), color='blue', alpha=0.3) -> Option A
  4. Quick Check:

    Use where=(y1 > y2) to fill selectively [OK]
Hint: Use where=(condition) to fill selectively [OK]
Common Mistakes:
  • Omitting the where parameter for conditional fill
  • Reversing the condition in where
  • Using equality instead of greater than
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