Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Show Normal Distribution in Statistics using Python
Normal distribution, also known as Gaussian distribution, is a fundamental probability distribution in statistics with a characteristic bell-shaped curve. Python provides powerful libraries to visualize and work with normal distributions effectively.
What is Normal Distribution in Statistics?
Normal distribution is a continuous probability distribution that is symmetric around its mean. It has several key properties:
Bell-shaped curve The distribution forms a symmetric bell shape
Mean, median, and mode All three are equal and located at the center
68-95-99.7 rule Approximately 68% of data falls within 1 standard deviation, 95% within 2, and 99.7% within 3
Plotting Normal Distribution Using Python
We can visualize normal distribution using NumPy, Matplotlib, and SciPy libraries. Here's the complete implementation ?
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
# Set the parameters
mean = 2
std_dev = 5
# Generate x-coordinate values
x = np.linspace(mean - 4 * std_dev, mean + 4 * std_dev, 1000)
# Calculate probability density function
y = norm.pdf(x, mean, std_dev)
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label=f'?={mean}, ?={std_dev}')
plt.fill_between(x, y, alpha=0.3)
# Add labels and title
plt.xlabel('Value')
plt.ylabel('Probability Density')
plt.title('Normal Distribution')
plt.legend()
plt.grid(True, alpha=0.3)
# Show the plot
plt.show()
Generating Random Samples
You can also generate random samples from a normal distribution and create a histogram ?
import numpy as np
import matplotlib.pyplot as plt
# Generate random samples
samples = np.random.normal(loc=2, scale=5, size=10000)
# Create histogram
plt.figure(figsize=(10, 6))
plt.hist(samples, bins=50, density=True, alpha=0.7, color='skyblue', edgecolor='black')
# Overlay the theoretical curve
x = np.linspace(samples.min(), samples.max(), 100)
y = norm.pdf(x, 2, 5)
plt.plot(x, y, 'r-', linewidth=2, label='Theoretical Normal Distribution')
plt.xlabel('Value')
plt.ylabel('Density')
plt.title('Histogram of Random Samples vs Theoretical Normal Distribution')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Comparing Different Normal Distributions
Let's compare normal distributions with different parameters ?
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
# Create x values
x = np.linspace(-10, 10, 1000)
# Different parameter combinations
params = [
(0, 1), # Standard normal
(0, 2), # Same mean, different std
(2, 1), # Different mean, same std
(-1, 0.5) # Different mean and std
]
# Create the plot
plt.figure(figsize=(12, 8))
for mean, std in params:
y = norm.pdf(x, mean, std)
plt.plot(x, y, linewidth=2, label=f'?={mean}, ?={std}')
plt.xlabel('Value')
plt.ylabel('Probability Density')
plt.title('Comparison of Different Normal Distributions')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters Summary
| Parameter | Symbol | Effect on Distribution |
|---|---|---|
| Mean | ? (mu) | Shifts the curve left or right |
| Standard Deviation | ? (sigma) | Controls the width/spread of the curve |
| Variance | ?² | Square of standard deviation |
Conclusion
Normal distribution is essential in statistics and can be easily visualized using Python's NumPy, Matplotlib, and SciPy libraries. The key is understanding how mean and standard deviation parameters affect the curve's position and shape.
