简体   繁体   English

如何将2D等高线图拟合到2D高斯

[英]how to fit 2D contour plot to 2D gaussian

I have a 2D contour plot and I want to fit it with 2D Gaussian. 我有一个2D等高线图,我想用2D高斯拟合它。 This is the script I used to plot the 2D contour 这是我用来绘制2D轮廓的脚本

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

How to fit it? 如何适应?

EDIT 编辑

Like this script which fit Gaussian to 1D histogram but I want to fit Gaussian to 2D histogram 像这个脚本一样,高斯适合一维直方图,但我想让高斯适合二维直方图 高斯拟合

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

If you want to fit a 2D Gaussian to your data x and y , it's very straightforward (the maximum likelihood estimates for the mean and covariance are built in to Numpy) -- eg as follows for random x and y data. 如果要将2D高斯拟合为数据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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM