我有一个如下所示的图表:
![](https://i.stack.imgur.com/ydlFW.png)
我为获取该图(8 个图的序列之一)而运行的代码如下:
date_list = list(df_testing_set['date'].unique())
random_date_list = list(np.random.choice(date_list,8))
df_new = df_testing_set[df_testing_set['date'].isin(random_date_list)]
for date1 in random_date_list:
df_new = df_testing_set[df_testing_set['date'] == date1]
title = date1
if df_new.iloc[0]['day'] in ['Saturday', 'Sunday']:
df_shader = df_result_weekend.copy()
title += " - Weekend"
else:
df_shader = df_result_weekday.copy()
title += " - Weekday"
y = df_new[row_index].tolist()
x = range(0, len(y))
x_axis = buckets
y_axis = df_shader.loc[df_shader.index.isin([row_index]) & df_shader['Bucket'].between(1, 144), data_field].tolist()
del y_axis[-1]
plt.title(title)
plt.xlabel("Time of Day (10m Intervals)")
plt.ylabel(data_field + " values for " + row_index)
standevs = df_shader.loc[df_shader.index.isin([row_index]) & df_shader['Bucket'].between(1, 144), 'StanDev'].tolist()
del standevs[-1]
lower_bound = np.array(y_axis) - np.array(standevs)
upper_bound = np.array(y_axis) + np.array(standevs)
plt.fill_between(x_axis, lower_bound, upper_bound, facecolor='lightblue')
#highlighting anomalies
# if (y > upper_bound | y < lower_bound):
# plt.plot(x,y, 'rx')
# else:
# plt.plot(x, y)
plt.plot(x,y)
plt.show()
del df_shader, title, date1, df_new
我正在尝试创建一个条件(如注释的 if 语句),以便当绘制的坐标高于阈值时upper_bound
或以下lower_bound
,这些点用不同颜色的“x”标记。我希望最终达到这样的效果:如果一个点超过阈值 1 个标准差,它将被标记为橙色,如果超过 2 个或更多标准差,它将被标记为红色。我有数据框中的所有标准差df_shader
列下StanDev
。每当我尝试运行 if 块的某些变体时,我都会收到变量错误和名称错误