Matplotlib Best Fit Curve in Python

I was working on a project where I had to analyze sales data for a retail chain in the USA. The challenge was to find a way to represent the trend in the data clearly.

While plotting the raw data points was easy, what I really needed was a smooth curve that best represented the overall trend. This is where the concept of a best fit curve in Matplotlib came into play.

In this tutorial, I’ll show you multiple ways to create a best-fit curve in Python using Matplotlib. I’ll explain each method step by step, with full code examples, so you can easily follow along.

What is a Best Fit Curve?

A best-fit curve is a line or curve that approximates the trend of data points in a dataset. It doesn’t pass through every point but tries to minimize the overall error.

In Python, we can generate best-fit curves using libraries like NumPy, SciPy, and Matplotlib. These tools make it simple to fit linear, polynomial, or even non-linear curves.

Method 1 – Best Fit Line Using Python NumPy Polyfit

The simplest way to create a best-fit curve is by using Python numpy.polyfit(). This method works great when you want a straight line or a polynomial curve.

Here’s a step-by-step example.

import numpy as np
import matplotlib.pyplot as plt

# Example dataset: Monthly sales numbers for a US retail store
months = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
sales = np.array([120, 150, 170, 200, 210, 250, 300, 280, 320, 330, 360, 400])

# Fit a linear trend line (degree = 1)
coefficients = np.polyfit(months, sales, 1)
polynomial = np.poly1d(coefficients)

# Generate y-values for the best fit line
best_fit_line = polynomial(months)

# Plotting
plt.scatter(months, sales, color='blue', label='Actual Sales')
plt.plot(months, best_fit_line, color='red', label='Best Fit Line')
plt.xlabel("Month")
plt.ylabel("Sales (in $1000s)")
plt.title("Monthly Sales Trend - Best Fit Line")
plt.legend()
plt.show()

You can see the output in the screenshot below.

Best Fit Curve in Python Matplotlib

In this example, I used polyfit with degree 1 to create a straight line. The red line shows the best fit curve, while the blue points represent the actual sales data.

Method 2 – Polynomial Best Fit Curve

Sometimes, the data doesn’t follow a straight line. In that case, we can fit a polynomial curve of a higher degree.

Here’s how you can do it.

import numpy as np
import matplotlib.pyplot as plt

# Example dataset: Average temperature over 12 months in New York
months = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
temperature = np.array([30, 32, 40, 55, 65, 75, 80, 78, 70, 58, 48, 35])

# Fit a polynomial curve (degree = 3)
coefficients = np.polyfit(months, temperature, 3)
polynomial = np.poly1d(coefficients)

# Generate smooth x values for curve
x_smooth = np.linspace(1, 12, 100)
y_smooth = polynomial(x_smooth)

# Plotting
plt.scatter(months, temperature, color='blue', label='Actual Temperature')
plt.plot(x_smooth, y_smooth, color='green', label='Best Fit Curve (Polynomial)')
plt.xlabel("Month")
plt.ylabel("Temperature (°F)")
plt.title("Average Monthly Temperature in New York")
plt.legend()
plt.show()

You can see the output in the screenshot below.

Matplotlib Best Fit Curve in Python

Here, I used a degree 3 polynomial to fit the temperature data. The curve follows the seasonal pattern much more closely than a straight line would.

Method 3 – Nonlinear Curve Fitting with Python SciPy

If your data follows a nonlinear pattern, you can use Python scipy.optimize.curve_fit() to define a custom function and fit it.

Let me show you with an example.

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Example dataset: Population growth in a US city
years = np.array([2000, 2005, 2010, 2015, 2020])
population = np.array([2.5, 2.9, 3.6, 4.5, 5.2])  # in millions

# Define an exponential growth function
def growth(x, a, b):
    return a * np.exp(b * (x - 2000))

# Fit the curve
params, _ = curve_fit(growth, years, population)

# Generate smooth curve
x_smooth = np.linspace(2000, 2020, 100)
y_smooth = growth(x_smooth, *params)

# Plotting
plt.scatter(years, population, color='blue', label='Actual Population')
plt.plot(x_smooth, y_smooth, color='orange', label='Best Fit Curve (Exponential)')
plt.xlabel("Year")
plt.ylabel("Population (millions)")
plt.title("Population Growth in US City")
plt.legend()
plt.show()

You can see the output in the screenshot below.

Matplotlib Best Fit Curve

In this case, I defined an exponential growth function to fit the population data. The resulting curve matches the growth trend very well.

Method 4 – Best Fit Curve with Python Seaborn

Another quick way to plot a best-fit curve is by using Python Seaborn, which is built on top of Matplotlib.

Here’s an example.

import seaborn as sns
import matplotlib.pyplot as plt

# Example dataset: Advertising budget vs sales revenue
budget = [10, 15, 20, 25, 30, 35, 40, 45]
revenue = [25, 28, 35, 40, 42, 50, 52, 60]

# Seaborn regression plot
sns.regplot(x=budget, y=revenue, ci=None, scatter_kws={"color": "blue"}, line_kws={"color": "red"})
plt.xlabel("Advertising Budget ($1000s)")
plt.ylabel("Revenue ($1000s)")
plt.title("Advertising Budget vs Revenue")
plt.show()

With just one line of code (sns.regplot), Seaborn automatically fits a regression line. This is the fastest method when you need a quick visualization without much customization.

Choose the Right Method

  • Use NumPy polyfit for linear or polynomial trends.
  • Use SciPy curve_fit when you need custom nonlinear models.
  • Use Seaborn regplot for quick regression visualizations.

Each method has its strengths, and the right choice depends on your dataset and the type of relationship you expect.

Key Takeaways

  • A best-fit curve helps reveal trends in your data.
  • Matplotlib works seamlessly with NumPy, SciPy, and Seaborn to create these curves.
  • Always choose the curve type that best matches your dataset’s behavior.

Creating a best-fit curve in Matplotlib is one of those tasks I find myself doing often. Whether it’s sales forecasting, analyzing temperature data, or modeling population growth, the ability to visualize trends makes decision-making much easier.

While there are multiple methods to achieve this, I recommend starting with numpy.polyfit for simple cases and moving to curve_fit for more complex models.

You may also read other matplotlib articles:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.