3

I would like to plot the Poisson function in Python using Matplotlib. 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 can I plot this graph? Thanks in advance

1
  • 1
    Should it be pow(5,i) and factorial(i) instead of t? Jul 9, 2018 at 10:01

2 Answers 2

3

i think your function is not 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

1
  • Note that scipy.misc.factorial is deprecated and is, as of 1.0, scipy.special.factorial. Nov 13, 2019 at 16:10
0

The immediate problem is probably that you are using 't' instead of 'i' in your loop. However, you may want to avoid mixing python lists with numpy arrays. You could do it like,

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

t = np.arange(0, 20, 0.1)
x = np.power(t, 5)
y = scipy.misc.factorial(t)

plt.plot( t, x / y, 'bs')
plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.