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?
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()
Check the color and alpha parameters in fill_between.
The fill_between function fills the area between line1 and line2 with the specified color and transparency. Here, color is green and alpha is 0.3, so the shaded area is green with 30% opacity.
Given the following code, how many points will be shaded between the two time series?
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()
Remember that fill_between shades between intervals, not just points.
The fill_between function shades the area between points, so for 10 points, there are 9 intervals shaded.
What error will this code produce?
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()
Check if all arrays have the same length.
The fill_between function requires all input arrays to have the same length. Here, line1 has length 3 but days and line2 have length 5, causing a ValueError.
You want to highlight the area where a time series is above 0.5 using fill_between. Which code snippet does this correctly?
Use the where parameter to specify the condition.
Option B correctly uses where=(y > 0.5) to fill only where the series is above 0.5. Other options fill incorrectly or fill everywhere.
What happens if you set the alpha parameter in fill_between to a negative value like -0.5?
Matplotlib clamps alpha values outside [0, 1] to the valid range.
The alpha parameter controls transparency. Matplotlib clamps values outside [0, 1] to that range. A negative value like -0.5 is clamped to 0, making the fill fully transparent so no shading is visible.