简体   繁体   English

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

[英]Plot a vertical line, in a time series, using pylab

my main question is how do i plot a vertical line when my x axis represents a datetime series? 我的主要问题是,当我的x轴表示日期时间序列时,如何绘制垂直线? eg 1st Jan to 12th Jan, 2106 例如2106年1月1日至1月12日

Some things I tried were: 我尝试过的一些事情是:

I already have a graph, made from a Dataframe with dates. 我已经有一个图,它是由带有日期的数据框制成的。 I need to plot a vertical line at x=somedate. 我需要在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()

I tried using 我尝试使用

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

However, it shows an error that ordinal line >=1. 但是,它显示顺序行> = 1的错误。 How can i plot? 我怎么画?

I update my comment above with some extra code. 我在上面用一些额外的代码更新了我的评论。 Following your code: 按照您的代码:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM