My dataframe
has two colums, say x
and y
. The graph below shows the scatter plot
of x
and y
.
Based on the scatter plot, I make a linear fitting using the following code which results in the blue straight line in the following image.
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.scatter(df['x'], df['y'])
b, m = polyfit(df['x'], df['y'], 1)
ax.plot(df['x'], b + m * df['x'], 'blue', linewidth=1)
Now, I want to make another fitting curve, maybe polynomial, of the scatter plot. The wanted result is something like the red curve in the above image. I tried using the following from here.
coefs = np.polyfit(df['x'], df['y'], 2)
p = np.poly1d(coefs)
plt.plot(df['x'], df['y'], "bo", markersize= 2)
plt.plot(df['x'], p(df['x']), "r-")
But the result is incorrect for my data as shown below.
How should I proceed?
Edit: The data is here.