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

时间:2016-02-29 17:01:39

标签: python matplotlib

我的主要问题是当x轴代表日期时间序列时,如何绘制垂直线?例如1月1日至1月12日,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 :(得分: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()
相关问题