首页 文章

使用pylab绘制时间序列中的垂直线

提问于
浏览
0

my main question is how do i plot a vertical line when my x axis represents a datetime series? e.g. 1st Jan to 12th Jan, 2106

我尝试过的一些事情是:

我已经有了一个由带有日期的Dataframe制作的图表 . 我需要在x = somedate处绘制一条垂直线 .

days = pd.DatetimeIndex(start='2016-01-07', end='2016-01-031', freq='D')
example=pd.DataFrame(np.arange(7,32),index=days)
gp=example.plot()

我试过用

gp.axvline(x=days[0].date())

但是,它显示序数行> = 1的错误 . 我该如何策划?

1 回答

  • 0

    我用一些额外的代码更新了上面的评论 . 按照你的代码:

    import numpy as np
    import pandas as pd
    import datetime as dt
    
    days = pd.DatetimeIndex(start='2016-01-07', end='2016-01-031', freq='D')
    example=pd.DataFrame(np.arange(7,32),index=days)
    gp=example.plot()
    
    gp.axvline(x=days[0].date())
    
    # And now the interesting part
    # I add one day to the previous left xlim
    gp.set_xlim(left = days[0].date() - dt.timedelta(days = 1))
    gp.figure.canvas.draw()
    gp.figure.show()
    

相关问题