为时间序列图添加趋势线

PV8*_*PV8 3 python numpy time-series trendline

datetime64[ns]我想在 python 中为时间序列图添加趋势线,这意味着当我关注此线程时,我的 x 轴(基准)的格式为: How to add trendline in python matplotlib dot (scatter) graphs?

并运行我的代码:

import numpy as np
#Trendlines
z = np.polyfit(df1['Datum'], df1['Score'], 1)
p = np.poly1d(z)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

UFuncTypeError: ufunc 'add' 不能使用类型为 dtype('

我该如何解决这个问题?该线程也没有帮助

PV8*_*PV8 7

解决方法是:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = mdates.date2num(df1['Datum'])
y= df1['Score']
z = np.polyfit(x, df1['Score'], 1)
p = np.poly1d(z)
#then the plot
df1.plot('Datum', 'Score')
plt.plot(x, p(x), "r--")
Run Code Online (Sandbox Code Playgroud)

用线图和趋势线给出结果