I have this dataframe:
df1.head()
Out[107]:
crashes date
0 90.0 2019-10-31
1 77.0 2019-10-30
2 93.0 2019-10-29
3 79.0 2019-10-28
4 72.0 2019-10-27
Now, I want to apply an anomaly detection operation to this data. I picked the SD-ESD method. Here is the script:
outliers_indices = sesd.seasonal_esd(df1.crashes,seasonality = 25, hybrid=True, max_anomalies=365, alpha = 3)
x= df1.crashes[:320]
y=df1.date[:320]
outliers = []
sorted_outliers_indices = np.sort(outliers_indices)
test_outliers_indices = sorted_outliers_indices
for idx in test_outliers_indices:
outliers.append(df1.crashes[idx])
marks = []
for i in x:
if i in outliers:
marks.append(i)
else:
marks.append(np.NAN)
plt.figure(figsize = (20,8))
plt.plot(x)
plt.plot(marks, 'ro', markersize = "3")
plt.legend(handles=[mpatches.Patch(color='#62A3C9', label='Crashes'), mpatches.Patch(color='red', label='Crash Anomaly')])
plt.ylabel('Crashes')
plt.xlabel('Date')
display()
My chart looks like this and as you can see, the Dates are not plotting in the right sequence. Instead, it uses data points indices.
When I tried plt.plot(x,y), it throws an "ValueError: view limit minimum -36457.6 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units."
My date columns is datetime64[ns]. Can someone help with this?