Matplotlib折线图未正确在X轴上绘制数据框的日期序列

我有这个数据框:

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

现在,我想对该数据进行异常检测操作。我选择了SD-ESD方法。这是脚本:

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()

我的图表看起来像这样,如您所见,日期没有按正确的顺序绘制。相反,它使用数据点索引。

Matplotlib折线图未正确在X轴上绘制数据框的日期序列

当我尝试plt.plot(x,y)时,它抛出“ ValueError:视图限制最小值-36457.6小于1,并且是无效的Matplotlib日期值。如果您将非datetime值传递给具有日期时间单位的轴。”

我的日期列是datetime64 [ns]。有人可以帮忙吗?

lujieqi 回答:Matplotlib折线图未正确在X轴上绘制数据框的日期序列

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3148685.html

大家都在问