How to Create a Poisson Probability Mass Function Plot in Python?
In this article, we will see how we can create a Poisson probability mass function plot in Python. In probability theory and statistics, the Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space if these events occur with a known constant mean rate and independently of the time since the last event. The Poisson distribution can also be used for the number of events in other specified intervals such as distance, area or volume.
In order to plot the Poisson distribution, we will use scipy module. SciPy is a free and open-source Python library used for scientific computing and technical computing. SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers and other tasks common in science and engineering.
In order to get the poisson probability mass function plot in python we use scipy’s poisson.pmf method.
Syntax : poisson.pmf(k, mu, loc)
Argument : It takes numpy array, shape parameter and location as argument
Return : It returns numpy array
Example 1:
Python3
# importing poisson fro scipyfrom scipy.stats import poisson # importing nupy as npimport numpy as np # importing matplotlib as pltimport matplotlib.pyplot as plt # creating a numy array for x-axisx = np.arange(0, 100, 0.5) # poisson distribution data for y-axisy = poisson.pmf(x, mu=40, loc=10) # plotting the graphplt.plot(x, y) # showing the graphplt.show() |
Output :

Example 2: Using step size of data as 1
Python3
# importing poisson fro scipyfrom scipy.stats import poisson # importing nupy as npimport numpy as np # importing matplotlib as pltimport matplotlib.pyplot as plt # creating a numy array for x-axis# using step size as 1x = np.arange(0, 100, 1) # poisson distribution data for y-axisy = poisson.pmf(x, mu=10, loc=40) # plotting the graphplt.plot(x, y) # showing the graphplt.show() |
Output:

Example 3: Plotting scatterplot for better viewing of data points
Python3
# importing poisson fro scipyfrom scipy.stats import poisson # importing nupy as npimport numpy as np # importing matplotlib as pltimport matplotlib.pyplot as plt # creating a numy array for x-axisx = np.arange(0, 100, 0.5) # poisson distribution data for y-axisy = poisson.pmf(x, mu=50, loc=0) # plotting thescatter plot graphplt.scatter(x, y) # showing the graphplt.show() |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


