高斯函数 Python

5

我想使用matplotlib绘制高斯函数。以下是我的代码:

    #!/usr/bin/env python
    from matplotlib import pyplot as plt
    import numpy as np
    import math

    def gaussian(x, alpha, r):
          return 1./(math.sqrt(alpha**math.pi))*np.exp(-alpha*np.power((x - r), 2.))

    x = np.linspace(-3, 3, 100)
    plt.plot(gaussian(x, 1, 0))

    plt.show()

为什么范围是从0到100而不是-3到3呢?enter image description here
1个回答

6
  • plot(y): 使用索引数组 x 为 0..N-1,绘制 y
  • plot(x, y): 使用默认的线条样式和颜色,绘制 x 和 y

plt.plot(gaussian(x, 1, 0)) 改为 plt.plot(x, gaussian(x, 1, 0))

输出:

enter image description here


太棒了!谢谢你! - Monica

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接