4

我从两个 linspaces 开始,然后对它们进行网格网格化。然后我在网格上计算一个函数的值。我的函数被称为cpt_hcpv(). 然后我想对我的数据进行热图,网格上的每个点都有对应的函数值。

代码看起来像

poro = np.linspace(min(poro), max(poro))
sw = np.linspace(min(sw), max(sw))

g = np.meshgrid(poro, sw)
points = zip(*(x.flat for x in g))

hcpv = []
for p in points:
    hcpv = hcpv + [cpt_hcpv(p[0], p[1], poro, sw)]

def cpt_hcpv(pCut, sCut, poro, sw):
    #find points belonging to calculation
    truncated = [(p, s) for p, s in zip(poro, sw) if p > pCut and s < sCut ]
    hcv = 0
    for k in truncated:
        hcv += p*(1-s)*0.5
    return hcv

为什么我不cpt_hcpv()直接在网格上计算:因为我必须在理解中处理条件,truncated = [(p, s) for p, s in zip(poro, sw) if p > pCut and s < sCut ]所以我必须迭代网格中的点。我不知道如何迭代网格网格。

所以,我想从 3d 坐标绘制热图:在points我有 x 和 y 的点和hcpv我有每个点的 z 参数,以相同的顺序。

从我发现的示例中,有 pylab 和 matplotlib 解决方案可以从网格网格 + 网格上计算的值绘制热图,方法以网格网格为参数。

有没有办法从 3d 坐标绘制热图?

4

2 回答 2

1

如果您需要遍历网格,请尝试以下操作:

g = np.meshgrid(poro, sw)

#Turn into 2x3x3 array
g_arr = np.array(g)                            

#Move first dimension to third: 3x3x2 array
g_arr = g_arr.swapaxes(0,1).swapaxes(1,2)

#Generate results by iterating over first and second dimension, and unpacking the third
hcpv = np.array([[cpt_hcpv(p, s, poro, sw) for p,s in r] for r in g_arr])

我不知道 matplotlib 是否会从通用 3-d 点轻松绘制热图。它必须处理分散、无序和缺失点的一般情况。

于 2013-08-26T18:37:16.480 回答
0

我使用 DrRobotNinja 方法提出了这个解决方案

g = np.meshgrid(poro_g, sw_g)
g_arr = np.array(g)                            
g_arr = g_arr.swapaxes(0,1).swapaxes(1,2)

#I compute z value on the grid, `g_arr`
hcpv = np.array([[cpt_hcpv(p, s, poro, sw) for p,s in r] for r in g_arr])

我叠加热图和轮廓(级别)

#heatmap
im = plt.imshow(hcpv, origin='lower',extent=[min(poro)-EPS,max(poro)
                                    +EPS,min(sw)-EPS,max(sw)+EPS],aspect='auto')

#contours
levels = np.array([p10,p50,p90])
cset = plt.contour(hcpv,levels,linewidths=2,cmap=pylab.cm.hot, 
      origin='lower',extent=[min(poro),max(poro),min(sw),max(sw)],aspect='auto')
plt.clabel(cset,inline=True,fmt='%1.1f',fontsize=20)

并显示

plt.show()
于 2013-08-31T16:12:44.563 回答