Table of Contents
The process of plotting the line of best fit in Python involves using a statistical method known as linear regression, which calculates the relationship between two variables and creates a straight line that best represents the data points. This line is then plotted on a graph using a Python library such as Matplotlib or Seaborn. The line of best fit can help visualize patterns and trends in the data and can be useful in making predictions. In order to plot the line of best fit, one must have a dataset with two variables and use Python code to perform the linear regression and plot the resulting line.
Plot Line of Best Fit in Python (With Examples)
You can use the following basic syntax to plot a line of best fit in Python:
#find line of best fit
a, b = np.polyfit(x, y, 1)
#add points to plot
plt.scatter(x, y)
#add line of best fit to plot
plt.plot(x, a*x+b)
The following example shows how to use this syntax in practice.
Example 1: Plot Basic Line of Best Fit in Python
The following code shows how to plot a basic line of best fit in Python:
import numpy as np
import matplotlib.pyplotas plt
#define data
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([2, 5, 6, 7, 9, 12, 16, 19])
#find line of best fit
a, b = np.polyfit(x, y, 1)
#add points to plot
plt.scatter(x, y)
#add line of best fit to plot
plt.plot(x, a*x+b) 
Example 2: Plot Custom Line of Best Fit in Python
The following code shows how to create the same line of best fit as the previous example except with the following additions:
- Customized colors for the points and the line of best fit
- Customized style and width for the line of best fit
- The equation of the fitted regression line displayed on the plot
import numpy as np
import matplotlib.pyplotas plt
#define data
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([2, 5, 6, 7, 9, 12, 16, 19])
#find line of best fit
a, b = np.polyfit(x, y, 1)
#add points to plot
plt.scatter(x, y, color='purple')
#add line of best fit to plot
plt.plot(x, a*x+b, color='steelblue', linestyle='--', linewidth=2)
#add fitted regression equation to plot
plt.text(1, 17, 'y = ' + '{:.2f}'.format(b) + ' + {:.2f}'.format(a) + 'x', size=14)
Feel free to place the fitted regression equation in whatever (x, y) coordinates you would like on the plot.
For this particular example, we chose (x, y) = (1, 17).
The following tutorials explain how to fit various regression models in Python:
Cite this article
stats writer (2024). How can I plot the line of best fit in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-the-line-of-best-fit-in-python/
stats writer. "How can I plot the line of best fit in Python?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-the-line-of-best-fit-in-python/.
stats writer. "How can I plot the line of best fit in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-the-line-of-best-fit-in-python/.
stats writer (2024) 'How can I plot the line of best fit in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-the-line-of-best-fit-in-python/.
[1] stats writer, "How can I plot the line of best fit in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I plot the line of best fit in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
