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)]
輸出:
添加回答
舉報