2 回答
TA贡献1811条经验 获得超4个赞
好的,这就是你要找的吗?
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)]

TA贡献1906条经验 获得超10个赞
这就是我将如何做到这一点。不确定我是否将垂直线放在正确的不连续处,但希望你能明白:
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)]
输出:

添加回答
举报
