【问题标题】:how to fit 2D contour plot to 2D gaussian如何将 2D 等高线图拟合到 2D 高斯
【发布时间】:2014-03-23 16:36:45
【问题描述】:

我有一个 2D 等高线图,我想用 2D Gaussian 拟合它。这是我用来绘制二维轮廓的脚本

import numpy as np
from pylab import *
from scipy.stats import kde
x = np.genfromtxt("deltaDEC.dat",delimiter="\n")
y = np.genfromtxt("cosDEC.dat",delimiter="\n")
n = len(x)
H, xedges, yedges = np.histogram2d(x, y, range=[[-40,40], [-40,40]], bins=(50, 50))
extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
levels = (400, 200, 100, 50, 20)
cset = contour(H, levels, origin='lower',colors=['black', 'pink','green','blue','red'],linewidths=(1.9, 1.6, 1.5, 1.4),extent=extent)
clabel(cset, inline=1, fontsize=10, fmt='%1.0i')
ylim(-10, 10)
xlim(-40, 40)
xlabel('delta_RA/cos(DEC)')
ylabel('delta_DEC')
for c in cset.collections:
  c.set_linestyle('solid')
colorbar()
show()

如何搭配?

编辑

喜欢这个脚本,它适合高斯到一维直方图,但我想把高斯适合二维直方图

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> import matplotlib.mlab as mlab
>>> from scipy.stats import norm
>>> data = np.loadtxt('delta DEC".txt')
>>> (mu,sigma) = norm.fit(data)
>>> plt.figure(1)
<matplotlib.figure.Figure object at 0x26bc350>
>>> n, bins, patches=plt.hist(data, 100, normed=True, histtype='step', facecolor='green')
>>> y = mlab.normpdf(bins, mu, sigma)
>>> plt.plot(bins, y, 'r--', linewidth=2)
[<matplotlib.lines.Line2D object at 0x31d0a10>]
>>> plt.xlabel('DEC difference[arcsec]')
<matplotlib.text.Text object at 0x31cb910>
>>> plt.ylabel('Probability')
<matplotlib.text.Text object at 0x2e8c3d0>
>>> plt.title('Gaussian distribution')
<matplotlib.text.Text object at 0x31d66d0>
>>> plt.grid(True)
>>> plt.show()

【问题讨论】:

    标签: python numpy matplotlib scipy


    【解决方案1】:

    如果您想将二维高斯拟合到您的数据 xy,这非常简单(Numpy 内置了均值和协方差的最大似然估计值)——例如随机xy数据如下。

    >>> import numpy as np
    >>> x, y = np.random.randn(2, 100)
    >>> data = np.vstack([x, y])
    
    >>> np.mean(data, axis=1)
    array([ 0.01154114, -0.01544327])
    
    >>> np.cov(data)
    array([[ 1.19047626, -0.11507689],
           [-0.11507689,  0.95112915]])
    

    【讨论】:

    • 我不是那个意思。我想将高斯拟合到等高线图。
    • 请你解释一下“[拟合]高斯到等高线图”是什么意思。 (对我来说这毫无意义:轮廓是从数据的直方图得出的,为什么不直接拟合数据?)另外你打算用拟合的高斯做什么?
    猜你喜欢
    • 2021-06-14
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2019-12-19
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多