KitDocumentation

BestFitLine

Creates a line of best fit through the given data

Examples

Example 1 - Add a Best Fit Line to Scatter Plot

After plotting two variables on a scatter plot, it is often useful to include a best fit line to help visualize the overall trend. In this example, we fit a line to the relationship between player Age and Points, and display it on the same plot to highlight the directional correlation in the data.
#> BestFitLine  --x Age --y Points --samePlot
AFLEFT  colorCycleIndex = colorCycleIndex + 1

coefficients = np.polyfit(nBADf['Age'], nBADf['Points'], 1)
p = np.poly1d(coefficients)
x_fit = np.linspace( ( nBADf['Age'] ).min(), ( nBADf['Age'] ).max(), 100)
y_fit = p(x_fit)
plt.plot(x_fit, y_fit, label=f'{coefficients[0]:.3f}x + {coefficients[1]:.3f}', color=colorCycle[colorCycleIndex])

plt.legend()  AFRIGHT