[SOLVED] Generating 2nd degree polynomial out of some data

Issue

I have some data which I want to generate a 2nd degree polyfit like this as example:
Polynomial i want to make

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.
Python plot

I have also tried with sklearn, and I can upload that if requested.

Solution

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)

Answered By – flurble

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *