How to plot a normal distribution with Matplotlib in Python?
Last Updated :
02 Apr, 2025
Normal distribution, also known as the Gaussian distribution, is a fundamental concept in probability theory and statistics. It is a symmetric, bell-shaped curve that describes how data values are distributed around the mean. The probability density function (PDF) of a normal distribution is given by:
Probability Density FunctionWhere, x is the variable, mu is the mean and sigma standard deviation. A special case of this function, known as the standard normal distribution, occurs when mu=0 and sigma=1:
Standard Normal DistributionWe can plot normal distribution in Python in various ways, let's see some of them:
Using scipy.stats.norm.pdf()
The scipy.stats.norm.pdf() function calculates the probability density function (PDF) for a normal distribution. This method allows for direct computation and visualization of the standard normal curve (mean = 0, standard deviation = 1). It is useful for precise statistical modeling and probability calculations.
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
x = np.linspace(-4, 4, 1000)
plt.plot(x, norm.pdf(x), 'b-', label='Normal Distribution')
plt.xlabel('X')
plt.ylabel('Probability Density')
plt.legend()
plt.grid(True)
plt.show()
Output
Standard Normal Distribution Explanation: This code generates and plots a standard normal distribution. It creates 1000 evenly spaced x-values from -4 to 4 using np.linspace(), computes their probability density with norm.pdf(x), and plots the result as a blue line. Axis labels, a legend and a grid are added for clarity and plt.show() displays the plot.
Using numpy
Instead of relying on SciPy, we can manually compute the normal distribution using NumPy. The probability density function (PDF) of a normal distribution is given by:
NumPy's Formula for Normal DistributionUsing NumPy, we evaluate this function over a range of values and plot the corresponding curve. This method provides a mathematical understanding of the normal distribution and is useful when working with custom probability distributions.
Python
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 1000)
y = np.exp(-x**2 / 2) / np.sqrt(2 * np.pi)
plt.plot(x, y, 'r-', label='Normal Distribution')
plt.xlabel('X')
plt.ylabel('Probability Density')
plt.legend()
plt.grid()
plt.show()
Output
Standard Normal Distribution Explanation: This code plots a standard normal distribution using its mathematical formula. It generates 1000 evenly spaced x-values from -4 to 4 with np.linspace(), then computes the probability density function (PDF) using the Gaussian formula exp(-x²/2) / sqrt(2π). The distribution is plotted as a red line, with labeled axes, a legend and a grid for clarity.
Using Seaborn sns.kdeplot()
Seaborn simplifies visualization by offering Kernel Density Estimation (KDE), which estimates the probability density of a dataset. The sns.kdeplot() function takes a randomly generated normal dataset and plots its density curve, providing a smoothed representation of the underlying distribution. This approach is ideal when working with real-world datasets that approximate a normal distribution.
Python
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data = np.random.normal(loc=0, scale=1, size=1000)
sns.kdeplot(data, color='green', label='Normal Distribution')
# Labels and title
plt.xlabel('X')
plt.ylabel('Density')
plt.legend()
plt.grid()
plt.show()
Output
Standard Normal Distribution Explanation: This code visualizes a normal distribution using Kernel Density Estimation (KDE) with Seaborn. It generates 1000 random values from a standard normal distribution (loc=0, scale=1) using np.random.normal(). The sns.kdeplot() function then estimates and plots the probability density in green. Labels, a legend and a grid are added for clarity.
Using histogram
histogram represents the frequency of data points within specific ranges (bins). By using Seaborn's sns.histplot() with kde=True, we can visualize both the histogram and its corresponding KDE curve, offering insights into data distribution patterns. This method is particularly useful for analyzing empirical data and identifying deviations from normality.
Python
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data = np.random.normal(loc=0, scale=1, size=1000)
sns.histplot(data, kde=True, bins=30, color='purple')
# Labels and title
plt.xlabel('X')
plt.ylabel('Frequency')
plt.grid()
plt.show()
Output
Standard Normal Distribution Explanation: It creates 1000 samples from a standard normal distribution (loc=0, scale=1) with np.random.normal(). The sns.histplot() function plots a histogram with 30 bins, overlays a Kernel Density Estimate (KDE) curve and colors it purple. Labels and a grid are added for clarity.