What is the purpose of highlighting date ranges in a plot?
Highlighting date ranges helps to visually emphasize specific periods in time, making it easier to spot trends, events, or anomalies within those intervals.
Click to reveal answer
beginner
Which matplotlib function is commonly used to highlight a date range on a plot?
The ax.axvspan(start_date, end_date, color=color, alpha=alpha) function is used to highlight vertical spans (date ranges) on a plot.
Click to reveal answer
beginner
How do you specify the transparency of a highlighted date range in matplotlib?
Use the alpha parameter in axvspan to set transparency. Values range from 0 (fully transparent) to 1 (fully opaque).
Click to reveal answer
intermediate
Why is it important to convert dates to matplotlib date format before highlighting?
Matplotlib needs dates in its internal numeric format to correctly position highlights on the x-axis. Using matplotlib.dates.date2num() converts datetime objects to this format.
Click to reveal answer
intermediate
What is a simple way to add multiple highlighted date ranges on the same plot?
Call ax.axvspan() multiple times with different start and end dates and colors to highlight several date ranges on the same plot.
Click to reveal answer
Which matplotlib function highlights a vertical date range on a plot?
Aplt.plot()
Bax.axhline()
Cax.axvspan()
Dax.fill_between()
✗ Incorrect
ax.axvspan() highlights vertical spans, perfect for date ranges on the x-axis.
What does the alpha parameter control in axvspan?
ATransparency level
BLine thickness
CColor hue
DDate format
✗ Incorrect
alpha sets transparency from 0 (invisible) to 1 (solid).
Before highlighting dates, why convert datetime objects using date2num()?
ATo convert dates to numeric format matplotlib understands
BTo change colors
CTo format labels
DTo sort dates
✗ Incorrect
Matplotlib needs numeric values to position highlights correctly on the axis.
If you want to highlight two separate date ranges, what should you do?
AUse one axvspan with combined dates
BChange the plot title
CUse axhline instead
DCall axvspan twice with different ranges
✗ Incorrect
Multiple calls to axvspan allow highlighting multiple date ranges.
What color parameter can you use in axvspan to highlight a date range?
Aalpha=0.5
Bcolor='red'
Clinewidth=2
Dlinestyle='--'
✗ Incorrect
The color parameter sets the fill color of the highlighted area.
Explain how to highlight a specific date range on a matplotlib plot step-by-step.
Think about converting dates and using axvspan with parameters.
You got /4 concepts.
Describe why highlighting date ranges can be useful in data visualization.
Consider how visual emphasis helps understanding.
You got /4 concepts.
Practice
(1/5)
1. What does the plt.axvspan(start_date, end_date) function do in matplotlib?
easy
A. It draws a horizontal line at start_date.
B. It highlights a vertical span (date range) on the plot between start_date and end_date.
C. It creates a scatter plot of dates between start_date and end_date.
D. It sets the x-axis limits to start_date and end_date.
Solution
Step 1: Understand the function purpose
plt.axvspan is used to highlight a vertical area on the plot, usually to mark a range on the x-axis.
Step 2: Match parameters to behavior
The parameters start_date and end_date define the start and end of this vertical span on the x-axis, highlighting that date range.
Final Answer:
It highlights a vertical span (date range) on the plot between start_date and end_date. -> Option B
Quick Check:
Highlight date range = plt.axvspan(start_date, end_date) [OK]
Hint: Remember axvspan highlights vertical areas between two x-values [OK]
Common Mistakes:
Confusing axvspan with line drawing functions
Thinking it changes axis limits
Assuming it plots points instead of highlighting
2. Which of the following is the correct syntax to highlight the date range from '2023-01-01' to '2023-01-10' with a semi-transparent red color in matplotlib?
easy
A. plt.axvspan(start='2023-01-01', end='2023-01-10', color='red', alpha=0.5)
B. plt.axvspan('2023-01-01', '2023-01-10', color='red', transparency=0.5)
C. plt.axvspan('2023-01-01', '2023-01-10', color='red', alpha=0.5)
D. plt.axvspan('2023-01-01', '2023-01-10', alpha=50, color='red')
Solution
Step 1: Check parameter names and order
plt.axvspan takes start and end positions as first two arguments, then keyword arguments like color and alpha.
Step 2: Validate correct keyword for transparency
alpha controls transparency and should be between 0 and 1. The value 0.5 means 50% transparent.
Final Answer:
plt.axvspan('2023-01-01', '2023-01-10', color='red', alpha=0.5) -> Option C
Quick Check:
Use alpha for transparency, not transparency keyword [OK]
Hint: Use alpha (0-1) for transparency, not transparency keyword [OK]
Common Mistakes:
Using 'transparency' instead of 'alpha'
Passing alpha as 50 instead of 0.5
Using named parameters start= and end= which are invalid
3. Given the following code, what will be the color and transparency of the highlighted date range?
A. A green shaded area with 30% opacity between Jan 2 and Jan 4
B. A green shaded area with 70% opacity between Jan 2 and Jan 4
C. A fully opaque green shaded area between Jan 2 and Jan 4
D. No shaded area will appear because alpha is too low
Solution
Step 1: Understand alpha parameter meaning
Alpha=0.3 means 30% opacity (or 70% transparent), so the color is faint but visible.
Step 2: Confirm color and date range
The color is green, and the highlighted range is from Jan 2 to Jan 4 as specified.
Final Answer:
A green shaded area with 30% opacity between Jan 2 and Jan 4 -> Option A
Quick Check:
alpha=0.3 means 30% opacity, color='green' [OK]
Hint: Alpha 0.3 means 30% opacity, not 70% [OK]
Common Mistakes:
Confusing alpha as transparency percentage
Thinking alpha=0.3 means 70% opacity
Assuming no highlight appears if alpha is low
4. Identify the error in the following code snippet that tries to highlight a date range:
import matplotlib.pyplot as plt
plt.axvspan('2023-01-01', '2023-01-05', color='blue', alpha=0.4)
plt.show()
medium
A. Dates are passed as strings; they should be converted to datetime objects.
B. The alpha value must be an integer, not a float.
C. The color parameter should be 'colour' instead of 'color'.
D. plt.show() is missing and must be added.
Solution
Step 1: Check parameter types for axvspan
plt.axvspan expects numeric or datetime values for start and end. Passing strings directly causes errors.
Step 2: Correct usage for dates
Convert strings to datetime objects using pd.Timestamp or datetime before passing.
Final Answer:
Dates are passed as strings; they should be converted to datetime objects. -> Option A
Quick Check:
axvspan needs datetime, not string dates [OK]
Hint: Convert date strings to datetime objects before axvspan [OK]
Common Mistakes:
Passing date strings directly without conversion
Misusing alpha as integer
Wrong color parameter spelling
5. You have a time series plot of daily sales data from '2023-01-01' to '2023-01-31'. You want to highlight weekends (Saturdays and Sundays) with a light gray transparent background. Which approach correctly applies this using matplotlib?
hard
A. Use plt.fill_between with weekend dates and color='gray', alpha=0.2.
B. Use plt.axvspan once with start='2023-01-01' and end='2023-01-31' and color='gray', alpha=0.2.
C. Plot vertical lines on weekends using plt.axvline with color='gray' and alpha=0.2.
D. Loop through dates, check if weekday is Saturday or Sunday, then use plt.axvspan for each weekend with color='gray' and alpha=0.2.
Solution
Step 1: Identify weekend dates in the range
Loop through each date, check if it is Saturday (weekday=5) or Sunday (weekday=6).
Step 2: Highlight each weekend separately
Use plt.axvspan for each weekend day or range with desired color and transparency.
Step 3: Understand why other options fail
Use plt.axvspan once with start='2023-01-01' and end='2023-01-31' and color='gray', alpha=0.2. highlights entire month, not weekends. Plot vertical lines on weekends using plt.axvline with color='gray' and alpha=0.2. draws lines, not spans. Use plt.fill_between with weekend dates and color='gray', alpha=0.2. is for y-axis fills, not x-axis spans.
Final Answer:
Loop through dates, check if weekday is Saturday or Sunday, then use plt.axvspan for each weekend with color='gray' and alpha=0.2. -> Option D
Quick Check:
Highlight weekends individually with axvspan [OK]
Hint: Highlight each weekend separately with axvspan in a loop [OK]
Common Mistakes:
Highlighting entire date range instead of weekends
Using axvline instead of axvspan for spans
Trying to fill y-axis area instead of x-axis date range
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.