Search code examples
pythonpandasdatetimematplotlibaxvline

axvline on a specific date - weekly data


I'm trying to create a vertical line on specific date, which is not part of my df weekly date index, and since it's not in the index, the plot is locating the vertical line on the next date that in the df index:

df=pd.DataFrame(index=pd.date_range('2020-01-01','2020-05-01',freq='W'),data={'val':range(0,17)})
ax=df.plot(grid=True,figsize=(12,6))
ax.set_xticks(df.index)
ax.set_xticklabels([x.date() for x in df.index],rotation=90)
ax.axvline(pd.Timestamp('2020-03-03'),ls='--',color='k')

enter image description here

as you can see, although I want to draw the line on '2020-03-03', it's created on '2020-03-08'.

Any ideas here? thanks :)


Solution

  • I was able to solve this using matplotlib instead of pandas plot

    import matplotlib.pyplot as plt
    fig,ax=plt.subplots(1,1,figsize=(12,6))
    ax.plot(df)
    
    ax.set_xticks(df.index)
    ax.set_xticklabels([x.date() for x in df.index],rotation=90)
    ax.axvline(pd.Timestamp('2020-03-03'),ls='--',color='k')
    

    enter image description here