0
0
Matplotlibdata~20 mins

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

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