📖 Scenario: You work as a data analyst for a weather station. You have daily temperature data for a week. You want to visualize the temperature changes and highlight the area between the minimum and maximum temperatures each day.
🎯 Goal: Create a time series plot of daily temperatures using matplotlib. Use fill_between to shade the area between the minimum and maximum temperatures for each day.
📋 What You'll Learn
Create lists for dates, minimum temperatures, and maximum temperatures.
Create a variable for the color to fill between the min and max temperatures.
Use plt.fill_between to shade the area between min and max temperatures.
Plot the min and max temperature lines on the same graph.
Display the plot with labels and a title.
💡 Why This Matters
🌍 Real World
Visualizing temperature ranges helps meteorologists and planners understand daily weather variations and prepare accordingly.
💼 Career
Data analysts and scientists often use time series plots with shaded areas to show ranges, confidence intervals, or variability in data.
Progress0 / 4 steps
1
Create the temperature data lists
Create three lists called dates, min_temps, and max_temps with these exact values: dates = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] min_temps = [12, 14, 13, 15, 14, 16, 15] max_temps = [20, 22, 21, 23, 22, 24, 23]
Matplotlib
Hint
Use square brackets [] to create lists with the exact values given.
2
Set the fill color for the shaded area
Create a variable called fill_color and set it to the string 'lightblue' to use as the color for the area between min and max temperatures.
Matplotlib
Hint
Assign the string 'lightblue' to the variable fill_color.
3
Plot the temperature lines and fill the area between
Import matplotlib.pyplot as plt. Use plt.fill_between with dates, min_temps, and max_temps and the color set to fill_color. Then plot the min_temps and max_temps lines using plt.plot with dates as x-axis. Add labels for x-axis as 'Day' and y-axis as 'Temperature (°C)'. Add the title 'Daily Min and Max Temperatures'.
Matplotlib
Hint
Remember to import matplotlib.pyplot as plt. Use plt.fill_between to shade between min and max temperatures. Then plot lines for min and max temperatures. Add labels and title for clarity.
4
Display the plot
Add a line to display the plot using plt.show().
Matplotlib
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. What does the fill_between function do in a time series plot using matplotlib?
easy
A. Creates a bar chart from time series data
B. Plots scatter points on the time series
C. Colors the area between two lines to highlight differences or ranges
D. Removes grid lines from the plot
Solution
Step 1: Understand the purpose of fill_between
The fill_between function 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 C
Quick 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
A. plt.fill_between(x, y1, y2, color='blue', alpha=0.3)
B. plt.fill_between(x, y1, color='blue', alpha=0.3)
C. plt.fill_between(y1, y2, x, color='blue')
D. plt.fill_between(x, y1, y2, linestyle='--')
Solution
Step 1: Check function parameters order
The correct order for fill_between is x (time points), then y1 and y2 (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 A
Quick Check:
fill_between(x, y1, y2) with color and alpha = C [OK]
Hint: Remember order: x, y1, y2 in fill_between [OK]
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 A
Quick 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
A. plt.fill_between(x, y1, y2, condition=(y1 > y2), color='orange')
B. plt.fill_between(x, y1, y2, where=(y1 > y2), color='orange', alpha=0.5)
C. plt.fill_between(x, y1, y2, where=(y1 < y2), color='orange', alpha=0.5)
D. plt.fill_between(x, y1, y2, color='orange', alpha=0.5)
Solution
Step 1: Use the 'where' parameter to specify condition
The where argument in fill_between allows filling only where the condition is True.
Step 2: Set condition to y1 > y2 and specify color and transparency
Using where=(y1 > y2) fills only days when y1 is higher than y2 with orange color and alpha 0.5.