Home > Enterprise >  Adding a vertical line to a time series plot in pandas df
Adding a vertical line to a time series plot in pandas df

Time:12-07

I used the answer to this question to try and add a vertical line to my time series plot: How do you plot a vertical line on a time series plot in Pandas?

This is my code:

ax = df.plot(figsize=(12,8), logy=True, title='Random Forest Regressor Performance',\
                 color={'price': 'blue', 'count': 'orange', 'pred': 'green'})

ax.axvline(pd.to_datetime('2021-08-23'), color='r', linestyle='--', lw=2)

And this is my data (I set the date (datetime) as an index):

date price pred count
2018-01-01 13657.20 12671.454 89709
2018-01-02 14982.10 18561.360 125144
2018-01-03 15201.00 14437.636 134138
... ... ... ...
2021-10-30 61888.10 39418.360 283597
2021-10-31 61318.00 34461.636 312403

In my case, using this method results in the line being put way out of the actual data. The idea is to put the line to this graph. What I get:

plot with line

Expected Result:

time series plot

CodePudding user response:

I assume that pandas does not set the axis format to date time, but converts the date-time values to number and changes the tick labels. Explicitly creating the axis and setting the correct format works.

Following this accepted-answer, the minimal example below shows how to set the date-time format of the axis and add the hline by using a corresponding date-time string.

import matplotlib.pyplot as plt
import pandas as pd

times = pd.date_range('2021-12-07', periods=500, freq='D')
yvalues = range(times.size)

df = pd.DataFrame(yvalues, times)

fig, ax = plt.subplots(1)
fig.autofmt_xdate()
ax.axvline(pd.to_datetime('2023-01-23'), color='r', linestyle='--', lw=2)
ax = df.plot(ax=ax, title='Random Forest Regressor Performance')
plt.show()

Which results in this plot

  • Related