0
0
Matplotlibdata~10 mins

Highlighting date ranges in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Highlighting date ranges
Prepare date data
Plot time series
Define date range to highlight
Use ax.axvspan() to add highlight
Show plot with highlighted range
This flow shows how to plot dates and highlight a specific date range on the plot using matplotlib.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import pandas as pd

dates = pd.date_range('2024-01-01', periods=10)
values = range(10)

plt.plot(dates, values)
plt.axvspan(pd.Timestamp('2024-01-03'), pd.Timestamp('2024-01-06'), color='yellow', alpha=0.3)
plt.show()
Plot a simple time series and highlight the date range from 2024-01-03 to 2024-01-06 with a yellow transparent box.
Execution Table
StepActionInput/ParametersResult/Output
1Create date range'2024-01-01' to 10 daysdates = 2024-01-01 to 2024-01-10
2Create valuesrange(10)values = [0,1,2,3,4,5,6,7,8,9]
3Plot dates vs valuesdates, valuesLine plot with 10 points
4Highlight date rangestart=2024-01-03, end=2024-01-06, color=yellow, alpha=0.3Yellow transparent box from 2024-01-03 to 2024-01-06 on plot
5Show plotNonePlot window opens with line and highlighted range
💡 Plot displayed with highlighted date range; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
datesNoneDatetimeIndex from 2024-01-01 to 2024-01-10SameSameSameSame
valuesNoneNone[0,1,2,3,4,5,6,7,8,9]SameSameSame
Key Moments - 3 Insights
Why do we use pd.Timestamp for the start and end dates in axvspan?
Matplotlib needs dates in a datetime format it understands. pd.Timestamp converts strings to datetime objects compatible with the plot's x-axis. See execution_table step 4.
What does the alpha parameter do in axvspan?
Alpha controls transparency of the highlight color. Lower alpha means more transparent. This lets us see the plot lines under the highlight. See execution_table step 4.
Can we highlight multiple date ranges on the same plot?
Yes, by calling axvspan multiple times with different start and end dates before plt.show(). Each call adds another highlighted area.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what color is used to highlight the date range?
ABlue
BRed
CYellow
DGreen
💡 Hint
Check the 'Input/Parameters' column in step 4 of execution_table.
According to variable_tracker, what is the value of 'values' after step 2?
A[0,1,2,3,4,5,6,7,8,9]
B[1,2,3,4,5,6,7,8,9,10]
CNone
DRange object
💡 Hint
Look at the 'values' row under 'After Step 2' in variable_tracker.
At which step does the plot window open showing the highlighted date range?
AStep 3
BStep 5
CStep 4
DAfter all steps
💡 Hint
Refer to the 'Result/Output' column in execution_table step 5.
Concept Snapshot
Highlighting date ranges in matplotlib:
- Use plt.plot() to plot dates vs values.
- Use plt.axvspan(start_date, end_date, color, alpha) to highlight range.
- Dates must be datetime objects (e.g., pd.Timestamp).
- Alpha controls transparency of highlight.
- Call plt.show() to display the plot.
Full Transcript
This lesson shows how to highlight date ranges on a matplotlib plot. First, we create a date range and some values. Then we plot these dates against values as a line. Next, we define a start and end date for the highlight and use axvspan with color and transparency to add a colored box behind the line plot. Finally, we show the plot window with the highlighted date range visible. Key points include converting date strings to datetime objects and adjusting transparency with alpha. Multiple highlights can be added by calling axvspan multiple times before showing the plot.