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
How to Create a Poisson Probability Mass Function Plot in Python?
The Poisson distribution is a probability distribution that models the occurrence of events in a fixed time or space interval, given the average rate of occurrence. It is commonly used in fields such as physics, engineering, and economics to model the arrival of particles, failures of components, or customer arrivals.
One way to visualize the Poisson distribution is to plot its probability mass function (PMF), which shows the probability of each possible number of events occurring in a given interval. In Python, we can use the SciPy library to generate the PMF of a Poisson distribution and then use Matplotlib to plot it.
In this article, we will explore how to create a Poisson PMF plot in Python using SciPy and Matplotlib. We will generate the PMF for different values of the Poisson parameter and visualize the results using a bar plot.
Using SciPy Library
The SciPy library provides the poisson.pmf() function to calculate Poisson probability mass function values. Here's how to create a basic Poisson PMF plot ?
Example
import numpy as np
from scipy.stats import poisson
import matplotlib.pyplot as plt
# Set the Poisson parameter and the range of k values
lam = 2
k_values = np.arange(0, 11)
# Calculate the PMF using the SciPy library
pmf_values = poisson.pmf(k_values, lam)
# Plot the PMF using Matplotlib
plt.figure(figsize=(10, 6))
plt.plot(k_values, pmf_values, 'bo-', linewidth=2, markersize=8)
plt.xlabel('Number of events (k)')
plt.ylabel('Probability')
plt.title('Poisson Probability Mass Function (?=2)')
plt.grid(True, alpha=0.3)
plt.show()
Displays a line plot with blue circles showing the Poisson PMF for ?=2
How It Works
Import libraries: NumPy for arrays, SciPy for statistical functions, and Matplotlib for plotting
Set parameters: Lambda (?) = 2 defines the average rate of events
Define range: k_values from 0 to 10 represent possible number of events
Calculate PMF:
poisson.pmf()computes probabilities for each k valueCreate plot: Line plot with markers shows the probability distribution
Using Bar Plot Visualization
A bar plot often provides better visualization for discrete probability distributions like Poisson ?
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import poisson
# Define the Poisson distribution parameter lambda
lam = 3
# Create an array of x values
x = np.arange(0, 15)
# Calculate the Poisson probability mass function
pmf = poisson.pmf(x, lam)
# Create the bar plot
plt.figure(figsize=(10, 6))
plt.bar(x, pmf, alpha=0.7, color='steelblue', edgecolor='black')
plt.title('Poisson Probability Mass Function (?=3)')
plt.xlabel('Number of events (k)')
plt.ylabel('Probability')
plt.grid(True, alpha=0.3)
plt.show()
Displays a bar chart showing the Poisson PMF for ?=3
Comparing Different Lambda Values
Let's compare Poisson distributions with different lambda values to understand how the parameter affects the distribution ?
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import poisson
# Define different lambda values
lambdas = [1, 3, 5]
x = np.arange(0, 15)
plt.figure(figsize=(12, 8))
for i, lam in enumerate(lambdas):
pmf = poisson.pmf(x, lam)
plt.subplot(2, 2, i+1)
plt.bar(x, pmf, alpha=0.7, color=f'C{i}')
plt.title(f'Poisson PMF (?={lam})')
plt.xlabel('Number of events (k)')
plt.ylabel('Probability')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Displays three subplots comparing Poisson distributions with ?=1, 3, and 5
Key Properties
| Lambda (?) | Mean | Variance | Shape |
|---|---|---|---|
| 1 | 1 | 1 | Right-skewed |
| 3 | 3 | 3 | Moderately skewed |
| 5 | 5 | 5 | Nearly symmetric |
Conclusion
Creating Poisson PMF plots in Python is straightforward using SciPy's poisson.pmf() function combined with Matplotlib. Bar plots work best for visualizing discrete probability distributions, and comparing different lambda values helps understand how the parameter affects the distribution shape and spread.
