I have some data which I want to generate a 2nd degree polyfit like this as example:
I have tried two different codes but the polynomial just trying to go through all points.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('TESTEXskelet.csv', sep=",")
x = data.Gennemsnitlig_hastighed
y1 = data.Sum_VSP
np.polyfit(x,y1,2)
plt.grid()
plt.title("VSP sum/hastighed")
plt.ylabel('VSP - kW/ton')
plt.xlabel('Hastighed - km/t')
plt.scatter(x,y1,s=5) # Definere selve plottet
plt.plot(x, y1)
But then it plots it through every point.
I have also tried with sklearn, and I can upload that if requested.
CodePudding user response:
You correctly fitted a 2nd degree polynomial. You are just not using it in the plot you do after that.
plt.scatter(x,y1,s=5)
does a scatter plot of your original data, and plt.plot(x, y1)
plots a line through all your data.
To plot the polynomial you need to catch the polynomial fit into a variable. Then define a range for the x-axis you want to plot over and predict y values based on the polynomial fit:
p = np.polyfit(x,y1,2)
xn = np.linspace(np.min(x), np.max(x), 100)
yn = np.poly1d(p)(xn)
plt.scatter(x,y1,s=5)
plt.plot(xn, yn)
CodePudding user response:
polyfit
returns the parameters to your polynomial, try
p = np.polyfit(x,y1,2)
y2 = np.polyval(p, x)
plt.plot(x, y2)