Open In App

Python - Gaussian fit

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Gaussian distribution also called a normal distribution. It is a common bell-shaped curve you see in lots of natural data, like people’s heights, IQ scores, or body temperatures. It’s named after the mathematician Carl Friedrich Gauss. The curve shows how likely different values are, with most values clustering around the average (mean) and fewer values far away from the mean.

How to plot Gaussian distribution in Python

Python’s NumPy, SciPy and Matplotlib libraries simplify generating and plotting Gaussian curves, whether for theoretical analysis or real data visualization.

Python
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

x = np.arange(-5, 5, 0.001) # x values from -5 to 5
y = norm.pdf(x, 0, 1)       

plt.plot(x, y)
plt.title('Normal Distribution (mean=0, std=1)')
plt.show()

Output:

Explanation: norm.pdf(x, 0, 1) gives the probability density function (PDF) for the normal distribution with mean=0 and standard deviation=1.

What if your data isn't perfect

Real-world data is often noisy and doesn't perfectly follow the ideal Gaussian shape. In such cases, we can fit a Gaussian curve to approximate the data using curve fitting techniques.

1. Custom Gaussian Function

To model such imperfect data, define a Gaussian function with customizable parameters:

Python
import numpy as np
def gauss(x, H, A, x0, sigma):
    return H + A * np.exp(-(x - x0)**2 / (2 * sigma**2))

print(gauss(np.array([-1,0,1]), 0, 1, 0, 1))

Output:

[0.60653066 1. 0.60653066]

Explanation: This function generates a customizable Gaussian (bell-shaped) curve for modeling real-world data. It takes input x (number or array) and parameters: H (baseline offset), A (peak height), x0 (center/mean) and sigma (controls curve width).

2. Using curve_fit to Fit Your Data

Python’s scipy.optimize.curve_fit helps find the best parameters (H, A, x0, sigma) to fit your data to the Gaussian curve.

Example 1: Fit simple data points

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

xdata = np.array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
ydata = np.array([1.2, 4.2, 6.7, 8.3, 10.6, 11.7, 13.5, 14.5, 15.7, 16.1, 16.6, 16.0, 15.4, 14.4, 14.2, 12.7, 10.3, 8.6, 6.1, 3.9, 2.1])

# Gaussian function
def Gauss(x, A, B):
    return A * np.exp(-B * x**2)

parameters, _ = curve_fit(Gauss, xdata, ydata)
fit_A, fit_B = parameters
fit_y = Gauss(xdata, fit_A, fit_B)

plt.plot(xdata, ydata, 'o', label='Data')
plt.plot(xdata, fit_y, '-', label='Fit')
plt.legend()
plt.show()

Output

Explanation: curve_fit finds the optimal parameters A and B to minimize the difference between ydata and the Gaussian model A * exp(-B * x²). The plot shows how well this curve fits the data.

Example 2: Fit noisy data with a full Gaussian

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

# Gaussian function
def func(x, a, x0, sigma):
    return a * np.exp(-(x - x0)**2 / (2 * sigma**2))

x = np.linspace(0, 10, 100)
y = func(x, 1, 5, 2)
yn = y + 0.2 * np.random.normal(size=len(x))  # Add noise

popt, _ = curve_fit(func, x, yn)
ym = func(x, *popt)

plt.plot(x, y, label='Original')
plt.scatter(x, yn, label='Noisy')
plt.plot(x, ym, label='Fitted')
plt.legend()
plt.show()

print("Fitted parameters:", popt)

Output:

Explanation: This code creates a Gaussian curve, adds noise and fits a Gaussian model to the noisy data using curve_fit. The plot shows the original curve, noisy points and the fitted curve. The printed parameters are the best-fit amplitude, center and width.


Practice Tags :

Similar Reads