0

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.

enter image description here

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.

k.ko3n
  • 954
  • 8
  • 26

1 Answers1

0

Code from @Dodge at this post gave me the solution.

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

y_values = df.loc[:, "data"]
x_values = np.linspace(0,1,len(df.loc[:, "data"]))
poly_degree = 1

coeffs = np.polyfit(x_values, y_values, poly_degree)
poly_eqn = np.poly1d(coeffs)
y_hat = poly_eqn(x_values)

plt.figure(figsize=(8,4))
plt.plot(df.loc[:, "Timestamp"], df.loc[:,"data"], "ro")
plt.plot(df.loc[:, "Timestamp"],y_hat)
plt.ylabel('data')
plt.xlabel('Year')
k.ko3n
  • 954
  • 8
  • 26