Python – Plot Poisson distribution in python

Plot Poisson distribution in python… here is a solution to the problem.

Plot Poisson distribution in python

I want to use Matplotlib to plot Poisson functions in Python. The function is (exp(-5)*5^x)/factorial(x)

import numpy as np
import math
import matplotlib.pyplot as plt

t = np.arange(0, 20, 0.1)
d = []

for i in t:
    p = pow(5,i)
    q = p/math.factorial(i)
    d.append(q)

plt.plot( t, np.exp(-5)*d, 'bs')
plt.show()

But I get this error “only size^1 arrays can be converted to Python scalars”. How do I draw this diagram? Thanks in advance

Solution

I don’t think your function is right: it’s exp(-5).

import numpy as np
import matplotlib.pyplot as plt
from scipy.special import factorial

t = np.arange(0, 20, 0.1)
d = np.exp(-5)*np.power(5, t)/factorial(t)

plt.plot(t, d, 'bs')
plt.show()

poisson distribution

Related Problems and Solutions