同样的结果集imshow可以绘制而pcolormesh不能

时间:2013-11-06 11:43:37

标签: python numpy matplotlib scipy kde

我正在使用scipy.stats.kde.gaussian_kde()进行kde分析,处理大量的点需要时间(对于100x点,250x250网格需要5分钟)。

作为gaussian_kde的更快替代品,我发现here写的fast_kde函数Joe Kington。 (加权kde也是选择fast_kde的一个因素)

相反绘制结果,我将其以格式(xmin,xmax,ymin,ymax,value)提取到文件中供以后使用。我使用this技术使用pcolormesh以原始形式提取结果。

以下是问题陈述: 由fast_kde函数为网格(500,500)生成的结果不能通过pcolormesh绘制,并且原始形式的输出也反映相同的无效结果,但是imshow方法完美地绘制了这个结果。

生成一些随机的二维数据:

from scipy import stats
def measure(n):
    "Measurement model, return two coupled measurements."
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2
m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()

对数据执行内核密度估算:

X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)

将结果保存到文件:(x,y,value)

fid = open('output.csv','w')
Z1 = (kernel(positions).T, X.shape)
Z = kernel(positions).T
#for currentIndex,elem in enumerate(positions):
for currentIndex,elem in enumerate(Z):
  #if Z1[currentIneex]>0:
  s1 = '%f %f %f\n'%(positions[0][currentIndex], positions[1][currentIndex], Z[currentIndex] )
  fid.write(s1)
fid.close()

打印结果:( minx,maxx,miny,maxy,value)

mshgrd = ax.pcolormesh(X,Y,Z) 
pths = mshgrd.get_paths() 
arr = mshgrd.get_array() 
for currentIndex,elem in enumerate(pths): 
 if arr[currentIndex]>0: bbox = elem.get_extents() 
 s2 = ",".join([str(i) for i in bbox.extents])+","+ str(arr[currentIndex]) +'\n' 
 print s2

绘制结果:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
          extent=[xmin, xmax, ymin, ymax])
ax.plot(m1, m2, 'k.', markersize=2)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
plt.show()

用于fast_kde的代码(问题区域

kernel = fast_kde(m1,m2,(500,500))
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
mshgrd = ax.pcolormesh(X,Y,kernel)
plt.show()

请建议我如何在此处添加图片(上传位置?)

0 个答案:

没有答案