We use fill_between to color the area between two lines in a time series plot. This helps to see the range or difference clearly.
Time series with fill_between in Matplotlib
Introduction
Syntax
Matplotlib
plt.fill_between(x, y1, y2=0, where=None, interpolate=False, color=None, alpha=None, label=None)
x is the time or x-axis values.
y1 and y2 are the two lines between which the area is filled.
Examples
values and zero with a light blue color.Matplotlib
plt.fill_between(dates, values, color='skyblue', alpha=0.5)
lower_bound and upper_bound to show a range.Matplotlib
plt.fill_between(dates, lower_bound, upper_bound, color='lightgreen', alpha=0.3)
series1 is greater than series2.Matplotlib
plt.fill_between(dates, series1, series2, where=(series1 > series2), color='red', alpha=0.4)
Sample Program
This code plots two time series lines and colors the area between them in light blue. It helps to see the difference between the two series over time.
Matplotlib
import matplotlib.pyplot as plt import pandas as pd import numpy as np # Create a date range dates = pd.date_range(start='2024-01-01', periods=10) # Create two time series series1 = np.array([10, 12, 15, 14, 13, 16, 18, 17, 19, 20]) series2 = np.array([8, 9, 11, 10, 12, 13, 14, 15, 16, 18]) plt.figure(figsize=(8,4)) plt.plot(dates, series1, label='Series 1', color='blue') plt.plot(dates, series2, label='Series 2', color='green') # Fill area between series1 and series2 plt.fill_between(dates, series1, series2, color='lightblue', alpha=0.5) plt.title('Time Series with fill_between') plt.xlabel('Date') plt.ylabel('Value') plt.legend() plt.tight_layout() plt.show()
Important Notes
Use alpha to control transparency so the lines remain visible.
The where parameter can highlight specific areas conditionally.
Make sure your x-axis values are sorted for correct filling.
Summary
fill_between colors the area between two lines in a plot.
It makes differences or ranges in time series easy to see.
Use transparency and conditions to customize the fill.
Practice
1. What does the
fill_between function do in a time series plot using matplotlib?easy
Solution
Step 1: Understand the purpose of fill_between
Thefill_betweenfunction is used to color the area between two lines or curves in a plot.Step 2: Connect to time series visualization
In time series plots, it highlights differences or ranges between two data series by filling the area between them.Final Answer:
Colors the area between two lines to highlight differences or ranges -> Option CQuick Check:
fill_between colors area between lines = A [OK]
Hint: Remember fill_between fills area between lines visually [OK]
Common Mistakes:
- Confusing fill_between with scatter plot
- Thinking it creates bar charts
- Assuming it removes grid lines
2. Which of the following is the correct syntax to fill the area between two time series arrays
y1 and y2 over time points x using matplotlib?easy
Solution
Step 1: Check function parameters order
The correct order forfill_betweenisx(time points), theny1andy2(values to fill between).Step 2: Validate optional arguments
Color and transparency (alpha) are valid optional arguments; linestyle is not used for fill_between.Final Answer:
plt.fill_between(x, y1, y2, color='blue', alpha=0.3) -> Option AQuick Check:
fill_between(x, y1, y2) with color and alpha = C [OK]
Hint: Remember order: x, y1, y2 in fill_between [OK]
Common Mistakes:
- Swapping y1 and y2 with x
- Using linestyle instead of color or alpha
- Omitting one of the y arrays
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt import numpy as np x = np.arange(5) y1 = np.array([1, 3, 2, 5, 4]) y2 = np.array([2, 4, 3, 6, 5]) plt.plot(x, y1, label='y1') plt.plot(x, y2, label='y2') plt.fill_between(x, y1, y2, color='green', alpha=0.4) plt.legend() plt.show()
medium
Solution
Step 1: Understand plot and fill_between behavior
The code plots two lines y1 and y2 over x, then fills the area between them with green color and transparency.Step 2: Confirm fill_between works with any order
fill_between fills area between y1 and y2 regardless of which is greater, so no error occurs.Final Answer:
A plot with two lines and a green shaded area between them -> Option DQuick Check:
Two lines + green fill_between area = A [OK]
Hint: fill_between shades area between lines regardless of order [OK]
Common Mistakes:
- Thinking fill_between needs y1 > y2 always
- Expecting no shaded area without fill_between call
- Confusing plot lines with fill_between shading
4. Identify the error in this code snippet that tries to fill between two time series:
import matplotlib.pyplot as plt x = [0, 1, 2, 3] y1 = [1, 2, 3] y2 = [2, 3, 4, 5] plt.fill_between(x, y1, y2, color='red') plt.show()
medium
Solution
Step 1: Check lengths of x, y1, and y2
x has 4 points, y1 has 3 points, y2 has 4 points; mismatch causes error.Step 2: Understand fill_between input requirements
fill_between requires x, y1, y2 arrays to have the same length to plot correctly.Final Answer:
x and y1 have different lengths causing a ValueError -> Option AQuick Check:
Length mismatch in inputs = B [OK]
Hint: Check all input arrays have same length before fill_between [OK]
Common Mistakes:
- Ignoring length mismatch errors
- Thinking fill_between needs plot calls first
- Assuming color argument case matters
5. You have two time series arrays
y1 and y2 representing daily temperatures for two cities over 7 days. You want to highlight only the days when y1 is higher than y2 using fill_between. Which code snippet correctly achieves this?hard
Solution
Step 1: Use the 'where' parameter to specify condition
Thewhereargument infill_betweenallows filling only where the condition is True.Step 2: Set condition to y1 > y2 and specify color and transparency
Usingwhere=(y1 > y2)fills only days when y1 is higher than y2 with orange color and alpha 0.5.Final Answer:
plt.fill_between(x, y1, y2, where=(y1 > y2), color='orange', alpha=0.5) -> Option BQuick Check:
Use where=(y1 > y2) to fill selectively = D [OK]
Hint: Use 'where' to fill conditionally between lines [OK]
Common Mistakes:
- Using 'condition' instead of 'where'
- Filling without condition fills entire area
- Reversing the condition (y1 < y2) by mistake
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
