Search code examples
pythonnumpytrigonometrybest-fit-curve

Polyfit degree from horizontal


In my project, I have a best-fit line for some points which I got using np.polyfit. With that, I want to calculate the degree between the returned best-fit line and the horizon. As if the first point in the best-fit line were at the origin. Some of my misunderstanding comes from confusion of what polyfit returns. How best could I get the angle?


Solution

  • import numpy as np
    # simulate some data:
    x = np.arange(10, dtype=np.float)
    b = 1.5
    y = 2 * x + b
    # fit the data with a 1st degree polynomial
    # (1st degree because poster mentions "best-fit *line*"):
    cf = np.polyfit(x, y, 1)
    np.rad2deg(np.arctan(cf[0]))
    

    It may be useful to watch https://www.brightstorm.com/math/precalculus/advanced-trigonometry/angle-inclination-of-a-line/ on how coefficients of the polynomial (cf[0] = m in the video) relate to the angle.