Python-计算区域KDE轮廓

时间:2018-12-21 13:43:17

标签: python contour kernel-density

我首先需要计算2D平面中轮廓的面积。我将使用this answer中提供的脚本来举例说明我的数据。我感兴趣的轮廓是最外面的(0.020个)。 enter image description here

现在我必须在该图像上叠加一个散点图,这样叠加看起来像这样 enter image description here

现在,在新图被“剪切”之后,我需要计算原始0.020轮廓的任何剩余面积。

计算出该切口的面积后,我必须计算下图的正方形之间的面积(顶点为(-2,-2),(2,-2),(2 ,2),(-2,2))以及蓝色的绘图:

enter image description here

这是用于生成图的所有代码。

import numpy as np
import matplotlib.pyplot as pl
import scipy.stats as st
import matplotlib.patches as patches


np.random.seed(0)  
data = np.random.multivariate_normal((0, 0), [[0.8, 0.05], [0.05, 0.7]], 100)
x = data[:, 0]
y = data[:, 1]
xmin, xmax = -3, 3
ymin, ymax = -3, 3

# Peform the kernel density estimate
xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([xx.ravel(), yy.ravel()])
values = np.vstack([x, y])
kernel = st.gaussian_kde(values)
f = np.reshape(kernel(positions).T, xx.shape)

fig = pl.figure()
ax = fig.gca()



ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
# Contourf plot
cfset = ax.contourf(xx, yy, f, cmap='Blues')
## Or kernel density estimate plot instead of the contourf plot
#ax.imshow(np.rot90(f), cmap='Blues', extent=[xmin, xmax, ymin, ymax])
# Contour plot
cset = ax.contour(xx, yy, f, colors='k')
# Label plot
ax.clabel(cset, inline=1, fontsize=10)
data = np.loadtxt('forbidden_parameters', unpack=True)

ax.scatter(data[1],data[2])
ax.set_xlabel('Y1')
ax.set_ylabel('Y0')

### drwa square
ax.add_patch(patches.Rectangle((-2, -2), 4, 4, facecolor='none'))



pl.show()

0 个答案:

没有答案
相关问题