I'm trying to add a trendline to my output plot. My dataframe looks something likes this:
Years Percentage increase
2021 1.480378
2022 1.270628
2023 0.42773
2024 2.271726
2025 3.857106
2026 2.123221
2027 2.658651
2028 2.786197
2029 2.106992
2030 2.959151
I'm trying to use the following code to get the trendline:
plt.plot(df['Years'], df['Percentage increase'])
x=df['Years']
y=df['Percentage increase']
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
pylab.plot(x,p(x),"r--")
But I'm getting the following error:
UFuncTypeError: Cannot cast ufunc 'lstsq_n' input 1 from dtype('O') to dtype('float64') with casting rule 'same_kind'
Can anyone help me out on how to resolve this issue?