How to Plot a Normal Distribution in Python

A normal distribution, also known as Gaussian distribution or bell curve, is a continuous probability distribution that describes data with a symmetrical bell-shaped curve. It is widely used in statistics to model real-world phenomena, such as human height, IQ scores, and errors in measurement. In this tutorial, we will explore how to generate and plot normal distributions using Python.

Mathematical Formulation

The probability density function (pdf) of a normal distribution is given by:

f(x) = (1 / (\sigma * \sqrt{2 * \pi})) * e^{-((x - \mu) ^ 2} / (2 * \sigma ^ 2))

Where:

  • x: a real number representing the data point
  • \mu: the mean or expected value of the distribution
  • \sigma: the standard deviation, which measures the spread of the distribution
  • e: the base of the natural logarithm, approximately equal to 2.71828
  • \pi: the mathematical constant, approximately equal to 3.14159265

Generating Normal Distributions in Python

Python’s NumPy library offers a convenient way to generate normal distributions using the numpy.random.normal function. Here’s an example:

import numpy as np

# Generate 1000 random numbers from a normal distribution with mean 50 and standard deviation 10
random_numbers = np.random.normal(50, 10, size=1000)

Visualizing Normal Distributions with Matplotlib

To visualize the generated normal distribution, we can use Python’s Matplotlib library. First, we’ll create a histogram of the random numbers:

import matplotlib.pyplot as plt

# Plot a histogram of the generated numbers
plt.hist(random_numbers, bins=50)
plt.title('Histogram of 1000 Random Numbers from a Normal Distribution')
plt.xlabel('Data Points')
plt.ylabel('Frequency')
plt.show()

Alternatively, we can plot the probability density function (pdf) of the normal distribution using the scipy.stats.norm function:

from scipy.stats import norm

# Generate 100 points between -3 and 3
x = np.linspace(-3, 3, 100)

# Plot the pdf of the normal distribution
plt.plot(x, norm.pdf(x, 50, 10))
plt.title('Probability Density Function of a Normal Distribution')
plt.xlabel('Data Points')
plt.ylabel('Probability Density')
plt.show()

By comparing the histogram and the pdf plot, we can see that the histogram approximates the bell-shaped curve of the normal distribution.

Leave a Reply

Your email address will not be published. Required fields are marked *