Skip to content

Python Normal Distribution PDF: Beginner's Guide to Effortlessly Understand and Apply


Python Normal Distribution PDF Tutorial


Welcome to this comprehensive tutorial on Python normal distribution probability density function (PDF). In this tutorial, we will explore the concept of a normal distribution and how to work with it in Python. We will cover the basics of probability density function, understand the characteristics of a normal distribution, and learn how to calculate the PDF using various methods.


The Python normal distribution PDF allows us to understand the probability of a given value falling within a certain range in a normal distribution. We can use this concept to analyze and model various real-world phenomena that follow a normal distribution pattern. In this tutorial, we will cover the basic concepts and provide a step-by-step guide to calculating the normal distribution PDF using Python.

1. What is a Normal Distribution?

A normal distribution, also known as a Gaussian distribution, is a probability distribution that follows a symmetric bell-shaped curve. It is characterized by its mean (µ) and standard deviation (σ) values. The curve is perfectly symmetrical around the mean, and the standard deviation determines the spread of the distribution.

2. Probability Density Function (PDF)

The probability density function (PDF) for a continuous random variable in a normal distribution describes the relative likelihood of the variable taking on a specific value. The PDF is represented by a curve and does not directly provide the probability of a single value occurring. Instead, it represents the probability density at a given point on the distribution curve.

3. Calculating PDF with scipy.stats

To calculate the PDF for a normal distribution in Python, we can use the scipy.stats module. First, you need to install the scipy package if it is not already installed in your Python environment. Use the following command to install:

Terminal window
pip install scipy

Once installed, you can import the relevant functions for working with the normal distribution PDF:

from scipy.stats import norm

4. Generating a Random Sample

Before calculating the PDF, let’s generate a random sample from a normal distribution. In this example, we will use numpy module to generate the random sample. Import the numpy module and generate a random sample:

import numpy as np
mu = 0 # mean
sigma = 1 # standard deviation
sample_size = 1000
sample = np.random.normal(mu, sigma, sample_size)

5. PDF Calculation with scipy.stats

To calculate the probability density function (PDF), we can use the pdf() function from the norm class of scipy.stats. This function takes the sample values and the distribution parameters (mean and standard deviation) as input and returns the PDF values:

pdf_values = norm.pdf(sample, mu, sigma)

Make sure you have imported the necessary function from scipy.stats earlier.

6. Visualizing the PDF

To visualize the PDF, we can use the matplotlib library. Import the relevant functions from matplotlib and plot the PDF:

import matplotlib.pyplot as plt
plt.hist(sample, density=True, bins=30, alpha=0.5) # Histogram of the sample
plt.plot(sample, pdf_values, color='red', linewidth=2) # Plotting the PDF
plt.ylabel('Probability Density')
plt.title('Normal Distribution PDF')

The density=True argument in the plt.hist() function normalizes the histogram, and alpha controls the transparency of the histogram bars. The plt.plot() function is used to plot the PDF curve over the histogram.

7. Probability Calculation with scipy.stats

In addition to PDF, we can also calculate the probability of a specific value falling within a certain range using the norm.cdf() function from scipy.stats. This function returns the cumulative probability up to a given value:

lower_value = -1
upper_value = 2
probability = norm.cdf(upper_value, mu, sigma) - norm.cdf(lower_value, mu, sigma)
print(f"The probability of the sample falling between {lower_value} and {upper_value} is {probability}")

8. Handling Non-Standard Normal Distributions

If you are working with a non-standard normal distribution, meaning a distribution with a different mean and standard deviation, you can still use the pdf() and cdf() functions by standardizing the values using the Z-score formula:

z = (x - mu) / sigma
# Calculation with standard normal distribution
pdf_value = norm.pdf(z)
cdf_value = norm.cdf(z)

Where x is the value you want to calculate the PDF or CDF for.

9. Multivariate Normal Distribution PDF

In addition to the univariate normal distribution, Python’s scipy.stats module also provides support for working with the multivariate normal distribution PDF. You can generate a multivariate sample and calculate the PDF using similar methods as before.

10. Further Resources

Congratulations! You have learned how to calculate the Python normal distribution PDF using various techniques. To deepen your understanding and explore more advanced concepts, you can refer to the following resources:


In this tutorial, we covered the basics of the normal distribution and explored how to calculate the probability density function (PDF) using Python. We learned to generate random samples, calculate PDF values, visualize the distribution, and calculate probabilities within a range. Additionally, we briefly touched on handling non-standard distributions and the multivariate normal distribution PDF.

By mastering the concepts and techniques discussed in this tutorial, you can apply them to solve real-world problems that involve modeling and analyzing data using the normal distribution in Python.

FAQs (Frequently Asked Questions)

Q1: What is the normal distribution in statistics? The normal distribution is a probability distribution that is symmetrical and follows a bell-shaped curve. It is widely used to model real-world phenomena in various fields.

Q2: How can I generate a large sample from a normal distribution? You can use the numpy module in Python to generate a random sample from a normal distribution. For example: np.random.normal(mean, standard_deviation, sample_size).

Q3: What does the standard deviation represent in a normal distribution? The standard deviation represents the average distance between each data point and the mean. It determines the spread or dispersion of the distribution.

Q4: How can I calculate the cumulative probability of a range in a normal distribution? You can use the norm.cdf() function from the scipy.stats module. Provide the range limits along with the mean and standard deviation to calculate the cumulative probability.

Q5: Can I use the Python normal distribution PDF for non-standard normal distributions? Yes, you can still use the pdf() and cdf() functions by standardizing the values using the Z-score formula. This allows you to work with non-standard normal distributions.