python matplotlib中的时间序列奇怪插值

python matplotlib中的时间序列奇怪插值,python,pandas,datetime,matplotlib,time-series,Python,Pandas,Datetime,Matplotlib,Time Series,我是matplotlib新手,正在处理的数据值仅在周一到周五的9到5区间内 当我试图绘制数据时,我注意到在下一个工作日的17:00和9:00之间的缺失值之间存在插值,这是因为matplotlib突然包含了缺失的日期时间 我的目标是: 1) 在不连续的左侧添加垂直虚线 2) 删除插值 下面是我正在使用的代码片段。代码将生成两个绘图 import numpy as np import pandas as pd import matplotlib.pyplot as plt from dateti

我是matplotlib新手,正在处理的数据值仅在周一到周五的
9
5
区间内

当我试图绘制数据时,我注意到在下一个工作日的
17:00
9:00
之间的缺失值之间存在插值,这是因为matplotlib突然包含了缺失的日期时间

我的目标是:

1) 在不连续的左侧添加垂直虚线

2) 删除插值


下面是我正在使用的代码片段。代码将生成两个绘图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from datetime import datetime


x_var1= pd.date_range(datetime(2014, 1, 14, 9, 0),datetime(2014, 1, 21, 17, 0),
                   freq="30min",
                   tz= 'US/Pacific',
                   closed= 'left'
                   )

x_var1 = x_var1[x_var1.dayofweek < 5]
x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= False)]
x_var1= x_var1[x_var1.hour != 12]

np.random.seed(0)
y_var2= np.random.normal(loc= 40, scale= 4.4, size= len(x_var1))
fig, ax= plt.subplots(nrows= 2, ncols= 1)

ax[0].plot(x_var1, y_var2)
ax[0].set_title("This plot has extra interpolation that needs to be removed and needs vertical dotted lines for discontinuities")


x_var2= np.arange(len(x_var1))
ax[1].plot(x_var2, y_var2)
ax[1].set_title("this plot only needs correct xaxis and added vertical dotted lines for discontinuities")

plt.show()
将numpy导入为np
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
从日期时间导入日期时间
x_var1=局部放电日期范围(日期时间(2014,1,14,9,0),日期时间(2014,1,21,17,0),
freq=“30分钟”,
tz=‘美国/太平洋’,
关闭='左'
)
x_var1=x_var1[x_var1.dayofweek<5]
x_var1=x_var1[x_var1.indexer_在时间之间('9:00','17:00',include_end=False)]
x_var1=x_var1[x_var1.hour!=12]
np.random.seed(0)
y_var2=np.随机.正常(loc=40,刻度=4.4,尺寸=len(x_var1))
图,ax=plt子批次(nrows=2,ncols=1)
ax[0]。绘图(x_var1,y_var2)
ax[0]。设置_标题(“此图有额外的插值,需要删除,不连续性需要垂直虚线”)
x_var2=np.arange(len(x_var1))
ax[1]。绘图(x_var2,y_var2)
ax[1]。设置_标题(“此图只需要正确的X轴,并为不连续添加垂直虚线”)
plt.show()
我的目标是使顶部曲线图看起来像底部曲线图,但底部曲线图需要将其X轴更改为顶部曲线图的X轴,并在不连续的左侧添加垂直虚线。我对matplotlib的世界还很陌生,所以我不知道如何以pythonical和高效的方式做这些事情


编辑:将示例数据集更改为更能反映实际问题的随机数据集。还包括午休时间,因此
12:00
12:59


以下是我将如何做到这一点。不确定我是否将垂直线放置在正确的不连续面上,但希望您能理解:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x_var1= pd.date_range(pd.to_datetime('2014-01-14 09:00:00'), pd.to_datetime('2014-01-21 17:00:00'),
                   freq="30min",
                   tz= 'US/Pacific',
                   closed= 'left'
                   )
x_var1 = x_var1[x_var1.dayofweek < 5]
x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= True)]

df = pd.DataFrame(index=x_var1, data=[np.nan]*len(x_var1))
df.iloc[0, 0] = 0
df.iloc[-1, 0] = 100
# to get line with "slope of 1"
df = df.interpolate(method='time')

# mpl won't plot between nan values
df[(df.index.hour == 17) & (df.index.minute == 0)] = np.nan
df[(df.index.hour == 9) & (df.index.minute == 0)] = np.nan
fig, ax = plt.subplots()
df.plot(ax=ax, legend=False)
df[(df.index.hour == 9) & (df.index.minute == 0)] = 0

# we know the discontinuities occur at the end of the days
[plt.axvline(d, ls='--') for d, v in list(zip(df.index, df.iloc[:, 0])) if pd.isnull(v)]
将numpy导入为np
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
x_var1=pd.date_范围(pd.to_datetime('2014-01-14 09:00:00')、pd.to_datetime('2014-01-21 17:00:00'),
freq=“30分钟”,
tz=‘美国/太平洋’,
关闭='左'
)
x_var1=x_var1[x_var1.dayofweek<5]
x_var1=x_var1[x_var1.indexer_在时间之间('9:00','17:00',包括_end=True)]
df=pd.DataFrame(index=x_var1,data=[np.nan]*len(x_var1))
df.iloc[0,0]=0
df.iloc[-1,0]=100
#要获得与“坡度为1”的直线
df=df.interpolate(method='time')
#mpl不会在nan值之间绘图
df[(df.index.hour==17)和(df.index.minute==0)]=np.nan
df[(df.index.hour==9)和(df.index.minute==0)]=np.nan
图,ax=plt.子批次()
df.绘图(ax=ax,图例=False)
df[(df.index.hour==9)和(df.index.minute==0)]=0
#我们知道不连续性发生在一天结束的时候
[plt.axvline(d,ls='--')表示列表中的d,v(zip(df.index,df.iloc[:,0]),如果pd.isnull(v)]
输出:


以下是我将如何做到这一点。不确定我是否将垂直线放置在正确的不连续面上,但希望您能理解:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x_var1= pd.date_range(pd.to_datetime('2014-01-14 09:00:00'), pd.to_datetime('2014-01-21 17:00:00'),
                   freq="30min",
                   tz= 'US/Pacific',
                   closed= 'left'
                   )
x_var1 = x_var1[x_var1.dayofweek < 5]
x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= True)]

df = pd.DataFrame(index=x_var1, data=[np.nan]*len(x_var1))
df.iloc[0, 0] = 0
df.iloc[-1, 0] = 100
# to get line with "slope of 1"
df = df.interpolate(method='time')

# mpl won't plot between nan values
df[(df.index.hour == 17) & (df.index.minute == 0)] = np.nan
df[(df.index.hour == 9) & (df.index.minute == 0)] = np.nan
fig, ax = plt.subplots()
df.plot(ax=ax, legend=False)
df[(df.index.hour == 9) & (df.index.minute == 0)] = 0

# we know the discontinuities occur at the end of the days
[plt.axvline(d, ls='--') for d, v in list(zip(df.index, df.iloc[:, 0])) if pd.isnull(v)]
将numpy导入为np
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
x_var1=pd.date_范围(pd.to_datetime('2014-01-14 09:00:00')、pd.to_datetime('2014-01-21 17:00:00'),
freq=“30分钟”,
tz=‘美国/太平洋’,
关闭='左'
)
x_var1=x_var1[x_var1.dayofweek<5]
x_var1=x_var1[x_var1.indexer_在时间之间('9:00','17:00',包括_end=True)]
df=pd.DataFrame(index=x_var1,data=[np.nan]*len(x_var1))
df.iloc[0,0]=0
df.iloc[-1,0]=100
#要获得与“坡度为1”的直线
df=df.interpolate(method='time')
#mpl不会在nan值之间绘图
df[(df.index.hour==17)和(df.index.minute==0)]=np.nan
df[(df.index.hour==9)和(df.index.minute==0)]=np.nan
图,ax=plt.子批次()
df.绘图(ax=ax,图例=False)
df[(df.index.hour==9)和(df.index.minute==0)]=0
#我们知道不连续性发生在一天结束的时候
[plt.axvline(d,ls='--')表示列表中的d,v(zip(df.index,df.iloc[:,0]),如果pd.isnull(v)]
输出:


好的,这就是你要找的吗

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x_var1= pd.date_range(pd.to_datetime('2014-01-14 09:00:00'), pd.to_datetime('2014-01-21 17:00:00'),
                   freq="30min",
                   tz= 'US/Pacific',
                   closed= 'left'
                   )

x_var1 = x_var1[x_var1.dayofweek < 5]
x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= True)]
x_var1= x_var1[x_var1.hour != 12]

np.random.seed(0)
y_var2= np.random.normal(loc= 40, scale= 4.4, size= len(x_var1))

df = pd.DataFrame(index=x_var1, data=y_var2)

# mpl won't plot between nan values
df[(df.index.hour == 17) & (df.index.minute == 0)] = np.nan
df[(df.index.hour == 9) & (df.index.minute == 0)] = np.nan
fig, ax = plt.subplots()
df.plot(ax=ax, legend=False)
df[(df.index.hour == 9) & (df.index.minute == 0)] = 0

# we know the discontinuities occur at the end of the days
[plt.axvline(d, ls='--') for d, v in list(zip(df.index, df.iloc[:, 0])) if pd.isnull(v)]
将numpy导入为np
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
x_var1=pd.date_范围(pd.to_datetime('2014-01-14 09:00:00')、pd.to_datetime('2014-01-21 17:00:00'),
freq=“30分钟”,
tz=‘美国/太平洋’,
关闭='左'
)
x_var1=x_var1[x_var1.dayofweek<5]
x_var1=x_var1[x_var1.indexer_在时间之间('9:00','17:00',包括_end=True)]
x_var1=x_var1[x_var1.hour!=12]
np.random.seed(0)
y_var2=np.随机.正常(loc=40,刻度=4.4,尺寸=len(x_var1))
df=pd.DataFrame(索引=x_var1,数据=y_var2)
#mpl不会在nan值之间绘图
df[(df.index.hour==17)和(df.index.minute==0)]=np.nan
df[(df.index.hour==9)和(df.index.minute==0)]=np.nan
图,ax=plt.子批次()
df.绘图(ax=ax,图例=False)
df[(df.index.hour==9)和(df.index.minute==0)]=0
#我们知道不连续性发生在一天结束的时候
[plt.axvline(d,ls='--')表示列表中的d,v(zip(df.index,df.iloc[:,0]),如果pd.isnull(v)]

好的,这就是你要找的吗

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x_var1= pd.date_range(pd.to_datetime('2014-01-14 09:00:00'), pd.to_datetime('2014-01-21 17:00:00'),
                   freq="30min",
                   tz= 'US/Pacific',
                   closed= 'left'
                   )

x_var1 = x_var1[x_var1.dayofweek < 5]
x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= True)]
x_var1= x_var1[x_var1.hour != 12]

np.random.seed(0)
y_var2= np.random.normal(loc= 40, scale= 4.4, size= len(x_var1))

df = pd.DataFrame(index=x_var1, data=y_var2)

# mpl won't plot between nan values
df[(df.index.hour == 17) & (df.index.minute == 0)] = np.nan
df[(df.index.hour == 9) & (df.index.minute == 0)] = np.nan
fig, ax = plt.subplots()
df.plot(ax=ax, legend=False)
df[(df.index.hour == 9) & (df.index.minute == 0)] = 0

# we know the discontinuities occur at the end of the days
[plt.axvline(d, ls='--') for d, v in list(zip(df.index, df.iloc[:, 0])) if pd.isnull(v)]
将numpy导入为np
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
x_var1=pd.date_范围(pd.to_datetime('2014-01-14 09:00:00')、pd.to_datetime('2014-01-21 17:00:00'),
freq=“30分钟”,
tz=‘美国/太平洋’,
关闭='左'
)
x_var1=x_var1[x_var1.dayofweek<5]
x_var1=x_var1[x_var1.indexer_在时间之间('9:00','17:00',包括_end=True)]
x_var1=x