How to plot a linear regression line on a scatter plot in Python

Plotting a linear regression line on a scatter plot displays a scatter plot of data along with a line of best fit for the data.

Solution for How to plot a linear regression line on a scatter plot in Python : You can use numpy.polyfit() to plot a linear regression line on a scatter plot Call numpy.polyfit(x, y, deg) with x and y as arrays of data for the scatter plot and deg as 1 to calculate the slope and y-intercept of the line of best fit. Plot the linear regression line by calling matplotlib.pyplot.plot(x, eq) with x as the array of x-values and eq as the y-intercept added to the product of the slope and x.


how-to-plot-a-linear-regression-line-on-a-scatter-plot-in-python