How to Plot the Line of Best Fit in Python

Spread the love

Plotting the line of best fit, also known as a trend line, can be a useful tool when analyzing data. It is a line that best represents the data by minimizing the distance between the line and all the data points in a scatter plot. In this article, we will explore how to plot the line of best fit using Python.

Step 1: Import Necessary Libraries

The libraries we’ll be using are numpy and matplotlib. If they’re not already installed, you can do so using pip or conda.

import numpy as np
import matplotlib.pyplot as plt

Step 2: Generate or Import Your Data

For simplicity, we’ll generate some data for this example. Let’s create a simple linear relationship with some random noise:

# Set a seed for reproducibility

# Generate data
X = np.linspace(0, 10, 100)
y = 3 * X + 2 + np.random.randn(100)

Here, we’ve created an array of 100 evenly spaced numbers between 0 and 10, and a corresponding array y that roughly follows the equation y = 3x + 2, with some random noise added.

Step 3: Plot the Data

Before we plot our line of best fit, let’s start by plotting our data:

# Create a scatter plot
plt.scatter(X, y)

Step 4: Calculate the Line of Best Fit

Next, we’ll calculate the line of best fit. Numpy’s polyfit function can do this for us. We’ll use it to fit a 1st degree polynomial (a line) to our data:

# Fit a 1st degree polynomial (a line) to the data
coefficients = np.polyfit(X, y, 1)

# This returns an array with the slope and intercept of the line
# coefficients[0] is the slope (m) and coefficients[1] is the intercept (c)
m, c = coefficients

Step 5: Plot the Line of Best Fit

Now that we have our line of best fit, we can plot it alongside our data. To do this, we’ll generate y-values for the line using the equation y = mx + c and plot this line:

# Generate y-values for the line of best fit
y_fit = m * X + c

# Create a scatter plot of the original data
plt.scatter(X, y)

# Plot the line of best fit
plt.plot(X, y_fit, 'r')

And there you have it – a scatter plot with a line of best fit!


The line of best fit can provide a simple visualization of the relationship between two variables. It can help with understanding trends in the data, predicting future outcomes, and identifying outliers. However, keep in mind that the line of best fit is a simplification of the data, and it assumes a particular type of relationship between the variables (linear, in this case). Always check whether this assumption makes sense given the context of your data and the question you’re trying to answer.

In Python, with the power of libraries like numpy and matplotlib, generating and plotting a line of best fit is a straightforward task. It’s worth noting that there are many other Python libraries, such as pandas and seaborn, which can provide even more functionality when it comes to data analysis and visualization.

Leave a Reply