Recently, while working on a data visualization project in Python, I had to highlight a specific date on a time series chart. The dataset contained daily stock prices for a U.S.-based company, and I needed to mark the date when a major event occurred, for example, a product launch or policy change.
At first, I thought there would be a direct option to mark the date on the chart. But, like many Python developers, I quickly realized that Matplotlib doesn’t have a one-click feature for this. However, there are simple ways to achieve it using built-in functions.
In this article, I’ll show you how to add vertical line at specific date in Matplotlib using Python. I’ll cover multiple methods, from using axvline() to using axvspan() for shaded regions, and share some practical tips I’ve learned from over a decade of working with Python and data visualization.
Method 1 – Add a Vertical Line Using axvline() in Matplotlib
The simplest and most common way to add a vertical line at a specific date in Matplotlib is by using the axvline() function. This function allows you to specify the x-coordinate (in our case, the date) where the vertical line should appear.
Example: Add a Vertical Line at a Specific Date
Below is a full working Python script that plots a time series and adds a vertical line at a given date.
# Import necessary libraries
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime, timedelta
# Generate sample data (U.S. stock prices for demonstration)
dates = pd.date_range(start="2025-01-01", periods=100)
prices = [100 + i * 0.5 + (i % 5) * 2 for i in range(100)] # Simulated price data
# Create a DataFrame
data = pd.DataFrame({"Date": dates, "Price": prices})
# Define the event date (e.g., product launch)
event_date = datetime(2025, 2, 15)
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(data["Date"], data["Price"], label="Stock Price", color="blue")
# Add a vertical line at the event date
plt.axvline(x=event_date, color="red", linestyle="--", linewidth=2, label="Event Date")
# Add labels and title
plt.title("Stock Price Trend with Event Date Marked", fontsize=14)
plt.xlabel("Date")
plt.ylabel("Price (USD)")
plt.legend()
# Rotate x-axis labels for better readability
plt.xticks(rotation=45)
# Display the plot
plt.tight_layout()
plt.show()You can see the output in the screenshot below.

In this example, the axvline() function is used to draw a red dashed vertical line on February 15, 2025. This is useful when you want to mark a specific date on a time series chart, for example, a policy announcement, earnings report, or product launch.
The color, linestyle, and linewidth parameters help you customize the appearance of the line. I often use a dashed red line for event markers because it stands out clearly.
Method 2 – Add a Vertical Line Using plt.vlines()
Another way to add a vertical line in Matplotlib is by using the plt.vlines() function. This method gives you more control over the start and end points of the line, which can be useful when you don’t want the line to span the entire height of the plot.
Example: Use plt.vlines() in Python
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
# Create sample data
dates = pd.date_range(start="2025-01-01", periods=60)
values = [50 + (i * 0.3) + (i % 7) * 1.5 for i in range(60)]
df = pd.DataFrame({"Date": dates, "Value": values})
# Define the event date
event_date = datetime(2025, 1, 30)
# Plot the data
plt.figure(figsize=(10, 6))
plt.plot(df["Date"], df["Value"], color="teal", label="Performance Metric")
# Add vertical line using vlines()
plt.vlines(x=event_date, ymin=min(df["Value"]), ymax=max(df["Value"]),
color="orange", linestyle="dotted", linewidth=2, label="Event Marker")
# Add chart details
plt.title("Performance Trend with Vertical Event Line", fontsize=14)
plt.xlabel("Date")
plt.ylabel("Metric Value")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()You can see the output in the screenshot below.

This approach is great when you need to control the height of the vertical line. The parameters ymin and ymax define where the line starts and ends along the y-axis. I often use this method when I want to highlight specific ranges or partial vertical lines within the chart.
Method 3 – Highlight a Date Range Using axvspan()
Sometimes, instead of marking a single date, you might want to highlight a range of dates, for example, a time period when a campaign or event was active. In such cases, Matplotlib’s axvspan() function is perfect.
Example: Highlight a Date Range in a Time Series Plot
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
# Sample data
dates = pd.date_range(start="2025-03-01", periods=90)
sales = [200 + (i * 1.2) + (i % 10) * 5 for i in range(90)]
df = pd.DataFrame({"Date": dates, "Sales": sales})
# Define the event period
start_date = datetime(2025, 4, 10)
end_date = datetime(2025, 4, 20)
# Plot the sales data
plt.figure(figsize=(10, 6))
plt.plot(df["Date"], df["Sales"], color="green", label="Daily Sales")
# Highlight the date range
plt.axvspan(start_date, end_date, color="yellow", alpha=0.3, label="Promotion Period")
# Add labels and legend
plt.title("Sales Trend Highlighting Promotion Period", fontsize=14)
plt.xlabel("Date")
plt.ylabel("Sales (USD)")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()You can see the output in the screenshot below.

Here, axvspan() shades the area between two dates, making it visually easy to identify the period of interest. The alpha parameter controls transparency, allowing the underlying data to remain visible.
I often use this method in Python dashboards where I need to show campaign durations, holiday periods, or anomaly windows in time series data.
Customize the Vertical Line in Matplotlib
Once you’ve added a vertical line, you can further customize it to improve readability and aesthetics. Here are some useful customization tips:
- Change color: Use named colors (“red”, “blue”) or hex codes (“#FF5733”).
- Adjust line style: Options include “solid”, “dashed”, “dotted”, or “dashdot”.
- Add labels: Use plt.text() to annotate the vertical line with a label.
- Combine with multiple lines: You can add multiple event markers by calling axvline() several times.
Here’s a quick example that combines these ideas.
Example: Multiple Event Lines with Labels
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
# Generate sample data
dates = pd.date_range(start="2025-01-01", periods=120)
values = [100 + (i * 0.4) + (i % 6) * 3 for i in range(120)]
df = pd.DataFrame({"Date": dates, "Value": values})
# Define multiple event dates
events = [
(datetime(2025, 2, 10), "Launch"),
(datetime(2025, 3, 5), "Update"),
(datetime(2025, 4, 15), "Sale")
]
# Plot the data
plt.figure(figsize=(12, 6))
plt.plot(df["Date"], df["Value"], color="navy", label="Performance")
# Add multiple vertical lines
for date, label in events:
plt.axvline(x=date, color="red", linestyle="--", linewidth=1.5)
plt.text(date, max(df["Value"]) - 5, label, rotation=90, color="red", fontsize=9,
verticalalignment="top", horizontalalignment="center")
# Add chart details
plt.title("Performance Trend with Multiple Event Dates", fontsize=14)
plt.xlabel("Date")
plt.ylabel("Value")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()This approach is especially useful in business or financial dashboards where you want to mark multiple key events. The combination of axvline() and plt.text() provides both visual and contextual clarity.
Common Mistakes to Avoid
While adding vertical lines in Matplotlib is easy, a few common mistakes can confuse:
- Using string dates without conversion: Always convert date strings to datetime objects before plotting.
- Forgetting to call tight_layout(): Without this, labels may overlap or get cut off.
- Not matching the date format: Ensure the date format in your dataset matches the x-axis format used by Matplotlib.
- Overusing bright colors: Too many bold colors can make your chart cluttered. Use contrast wisely.
Adding a vertical line at a specific date in Matplotlib using Python is a simple yet powerful way to emphasize important events in your time series data. Whether you’re marking a single date with axvline(), drawing partial lines with vlines(), or highlighting a range with axvspan(), these techniques can make your visualizations far more insightful.
I’ve used these methods countless times in Python projects, from financial analysis to operational dashboards, and they’ve always helped communicate key insights more effectively.
If you’re working with time series data in Python, don’t forget to explore other Matplotlib functions like annotate() and fill_between() for even more customization.
You may like to read:
- Matplotlib plot_date for Scatter and Multiple Line Charts
- Change Linestyle and Color in Matplotlib plot_date() Plots
- Date Format and Convert Dates in Matplotlib plot_date
- Control Date on X-Axis and Xticks in Matplotlib plot_date

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.