I want to linear fit an 8 years time-series data showing years in x-axis label.
The issue arises because I have TZ-aware
datetime column in my dataframe
. So, I cannot use my fitting codes directly with this type of timestamp. The file is here. This is what I did.
# Convert TZ-aware timestamp intu numbers
ts = df['Timestamp'].apply(lambda x:x.toordinal()).sort_values()
# Fit
from numpy.polynomial.polynomial import polyfit
y = df.data
x = ts
fig, (ax) = plt.subplots(1,1, figsize=(10, 6))
ax.scatter(x, y)
b, m = polyfit(x, y, 1)
plt.plot(x, b + m * x, 'blue', linewidth=1.5)
plt.show()
The result is below.
As expected, it works. But, the x-axis shows numbers instead of years. I want to show the month-year label on the x-axis.
Any help would be great and thanks in advance.