0
0
Matplotlibdata~20 mins

Time series with fill_between in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fill Between Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this fill_between plot code?

Consider the following Python code using matplotlib to plot a time series with shaded area between two lines. What will be the color of the shaded area?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

days = np.arange(5)
line1 = np.array([1, 3, 2, 5, 4])
line2 = np.array([0, 2, 1, 3, 2])

plt.fill_between(days, line1, line2, color='green', alpha=0.3)
plt.show()
AA green shaded area with full opacity between the two lines
BA blue shaded area with 30% opacity between the two lines
CNo shaded area because fill_between requires y1 > y2 always
DA green shaded area with 30% opacity between the two lines
Attempts:
2 left
💡 Hint

Check the color and alpha parameters in fill_between.

data_output
intermediate
2:00remaining
How many points are shaded in this fill_between example?

Given the following code, how many points will be shaded between the two time series?

Matplotlib
import numpy as np
import matplotlib.pyplot as plt

days = np.arange(10)
line1 = np.sin(days / 2) + 2
line2 = np.cos(days / 2) + 1

plt.fill_between(days, line1, line2)
plt.show()
A11 points are shaded, including start and end
B10 points are shaded, one for each day
C9 points are shaded, because fill_between shades between intervals
DNo points are shaded because line1 is always below line2
Attempts:
2 left
💡 Hint

Remember that fill_between shades between intervals, not just points.

🔧 Debug
advanced
2:00remaining
What error does this fill_between code raise?

What error will this code produce?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

days = np.arange(5)
line1 = np.array([1, 2, 3])
line2 = np.array([0, 1, 2, 3, 4])

plt.fill_between(days, line1, line2)
plt.show()
AValueError due to mismatched array lengths
BTypeError because days is not a list
CNo error, plot shows with missing points
DIndexError due to out of range access
Attempts:
2 left
💡 Hint

Check if all arrays have the same length.

🚀 Application
advanced
2:00remaining
Which option produces a plot with fill_between highlighting values above a threshold?

You want to highlight the area where a time series is above 0.5 using fill_between. Which code snippet does this correctly?

Aplt.fill_between(x, y, 0.5, where=(y < 0.5), color='red', alpha=0.4)
Bplt.fill_between(x, y, 0.5, where=(y > 0.5), color='red', alpha=0.4)
Cplt.fill_between(x, y, 0.5, color='red', alpha=0.4)
Dplt.fill_between(x, y, 0.5, where=(y == 0.5), color='red', alpha=0.4)
Attempts:
2 left
💡 Hint

Use the where parameter to specify the condition.

🧠 Conceptual
expert
2:00remaining
What is the effect of using fill_between with negative alpha values?

What happens if you set the alpha parameter in fill_between to a negative value like -0.5?

AThe fill is fully transparent, so no shading is visible
BMatplotlib raises a ValueError because alpha must be between 0 and 1
CThe fill is rendered with 50% opacity ignoring the negative sign
DThe fill color is inverted due to negative alpha
Attempts:
2 left
💡 Hint

Matplotlib clamps alpha values outside [0, 1] to the valid range.