Я могу предложить этот подход, используя fill_between
, используя аргумент where
:
Timestamp = pd.date_range('2020-02-06 08:23:04', periods=1000, freq='s')
df = pd.DataFrame({'Timestamp': Timestamp,
'Temperature': 30+15*np.cos(np.linspace(0,10,Timestamp.size))})
df['top_lim'] = 40.
df['bottom_lim'] = 25.
plt.plot_date(df['Timestamp'], df['Temperature'], '-')
plt.plot_date(df['Timestamp'], df['top_lim'], '-', color='r')
plt.plot_date(df['Timestamp'], df['bottom_lim'], '-', color='blue')
plt.fill_between(df['Timestamp'], df['bottom_lim'], df['Temperature'],
where=(df['Temperature'] >= df['bottom_lim'])&(df['Temperature'] <= df['top_lim']),
facecolor='orange', alpha=0.3)
########### EDIT ################
# plt.fill_between(df['Timestamp'], df['bottom_lim'], df['top_lim'],
# where=(df['Temperature'] >= df['top_lim']),
# facecolor='orange', alpha=0.3)
mask = (df['Temperature'] <= df['top_lim'])&(df['Temperature'] >= df['bottom_lim'])
plt.scatter(df['Timestamp'][mask], df['Temperature'][mask], marker='.', color='black')
cumulated_time = df['Timestamp'][mask].diff().sum()
plt.title(f'Cumulated time in range = {cumulated_time}')
plt.show()