Challenge - 5 Problems
Area Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Remember, plt.fill_between fills the area between the x-axis and the y-values.
✗ Incorrect
The function plt.fill_between fills the area between the x-axis (default y=0) and the y-values given. Here, it creates a blue shaded area under the curve defined by points (x, y).
🧠 Conceptual
intermediate1:30remaining
Understanding the role of y2 in plt.fill_between
In
plt.fill_between(x, y1, y2), what does the parameter y2 represent?Attempts:
2 left
💡 Hint
Think about the area being filled between two curves.
✗ Incorrect
In plt.fill_between, y1 and y2 define the two boundaries between which the area is filled. y1 is the upper boundary by default, and y2 is the lower boundary. If y2 is omitted, it defaults to zero (the x-axis).
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check if all input lists have the same length.
✗ Incorrect
The lists x and y1 have different lengths (4 vs 3), which causes a ValueError because plt.fill_between requires all input arrays to have the same length.
❓ data_output
advanced2: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()
Attempts:
2 left
💡 Hint
Compare y1 and y2 values element-wise.
✗ Incorrect
All elements in y1 are greater than corresponding elements in y2, so the area is filled for all 5 points.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Use the 'where' parameter to separate regions based on condition.
✗ Incorrect
Option A uses the 'where' parameter to fill the area between y1 and y2 with blue where y1 >= y2 and red where y1 < y2, correctly showing overlapping and non-overlapping areas with different colors.