【问题标题】:Plot a poisson distribution graph in python在 python 中绘制泊松分布图
【发布时间】:2018-07-09 09:57:35
【问题描述】:

我想使用 Matplotlib 在 Python 中绘制泊松函数。函数为 (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()

但我收到此错误。“只有 size^1 数组可以转换为 Python 标量”。如何绘制此图?提前致谢

【问题讨论】:

  • 应该是pow(5,i)factorial(i) 而不是t

标签: python matplotlib plot graphing


【解决方案1】:

我认为你的功能不对:它是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()

【讨论】:

  • 请注意,scipy.misc.factorial 已弃用,从 1.0 开始,scipy.special.factorial
【解决方案2】:

直接的问题可能是您在循环中使用了“t”而不是“i”。但是,您可能希望避免将 python 列表与 numpy 数组混合。你可以这样做,

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()

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多