Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How do you plot a vertical line on a time series plot in Pandas?
When working with time series data in Pandas, you often need to highlight specific dates or events by adding vertical lines to your plots. This can be achieved using matplotlib's axvline() method on the plot axes.
Creating a Time Series DataFrame
First, let's create a sample time series dataset with dates as the index ?
import pandas as pd
import matplotlib.pyplot as plt
# Create a DataFrame with date range
df = pd.DataFrame(index=pd.date_range("2019-07-01", "2019-07-31"))
df["value"] = range(1, 32) # Sample values for each day
print(df.head())
value
2019-07-01 1
2019-07-02 2
2019-07-03 3
2019-07-04 4
2019-07-05 5
Adding Vertical Lines to Time Series Plot
Use axvline() method to add vertical lines at specific dates ?
import pandas as pd
import matplotlib.pyplot as plt
# Create time series data
df = pd.DataFrame(index=pd.date_range("2019-07-01", "2019-07-31"))
df["value"] = range(1, 32)
# Create the plot
ax = df.plot(figsize=(10, 6))
# Add vertical lines at specific dates
ax.axvline("2019-07-15", color="green", linestyle="dashed", label="Mid-month")
ax.axvline("2019-07-25", color="red", linestyle="dashed", label="Important date")
# Customize the plot
plt.title("Time Series with Vertical Reference Lines")
plt.xlabel("Date")
plt.ylabel("Value")
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Multiple Vertical Lines with Different Styles
You can add multiple vertical lines with various styling options ?
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
df = pd.DataFrame(index=pd.date_range("2019-07-01", "2019-07-31"))
df["sales"] = [20, 25, 30, 28, 35, 40, 38, 42, 45, 48, 50, 52, 55, 53, 58,
60, 62, 65, 63, 68, 70, 72, 75, 73, 78, 80, 82, 85, 83, 88, 90]
ax = df.plot(kind='line', figsize=(12, 6))
# Add different styled vertical lines
ax.axvline("2019-07-10", color="blue", linestyle="-", linewidth=2, alpha=0.7, label="Campaign Start")
ax.axvline("2019-07-20", color="orange", linestyle="--", linewidth=1.5, label="Promotion")
ax.axvline("2019-07-28", color="red", linestyle=":", linewidth=2, label="Month End")
plt.title("Sales Data with Event Markers")
plt.xlabel("Date")
plt.ylabel("Sales")
plt.legend()
plt.tight_layout()
plt.show()
Line Style Options
| Parameter | Options | Description |
|---|---|---|
| linestyle | '-', '--', ':', '-.' | Solid, dashed, dotted, dash-dot |
| color | 'red', 'blue', '#FF5733' | Color name or hex code |
| linewidth | 1, 2, 3, etc. | Thickness of the line |
| alpha | 0.0 to 1.0 | Transparency level |
Conclusion
Use axvline() on your plot axes to add vertical reference lines at specific dates in time series plots. This method accepts various styling parameters like color, linestyle, and linewidth to customize the appearance of your reference lines.
Advertisements
