[英]Python heatmap from 3d coordinates
I start from two linspaces and I meshgrid them. 我从两个linspaces开始,我将它们网格化。 Then I calculate a function's values on grid. 然后我在网格上计算函数的值。 My function is called cpt_hcpv()
. 我的函数叫做cpt_hcpv()
。 Then I would like to heatmap my data, with each point on the grid with its corresponding function value. 然后我想热映射我的数据,网格上的每个点都有相应的函数值。
Code looks like 代码看起来像
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)]
with 同
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
Why I am not computing cpt_hcpv()
directly on grid: because I have to deal with condition in comprehension truncated = [(p, s) for p, s in zip(poro, sw) if p > pCut and s < sCut ]
so that I must iterate on the point in grid. 为什么我不直接在网格上计算cpt_hcpv()
:因为我必须处理comprehension中的条件truncated = [(p, s) for p, s in zip(poro, sw) if p > pCut and s < sCut ]
so我必须迭代网格中的点。 I don't know how to iterate on a meshgrid. 我不知道如何迭代网格网格。
So, I would like to heatmap from the 3d coordinates : in points
I have x and y for the points and in hcpv
I have the z parameters for each point, in same order. 所以, 我想从三维坐标中进行热图 :在points
我有x和y表示点,而在hcpv
我有每个点的z参数,顺序相同。
From the examples I have found, there are pylab and matplotlib solutions to plot heatmap from meshgrid + values computed on the grid, with a method taking meshgrid as an argument. 从我发现的例子中,有一个pylab和matplotlib解决方案,用于绘制网格上的热图,在网格上计算的值,以及一个以meshgrid为参数的方法。
Is there a way to plot heatmap from 3d coordinates ? 有没有办法从3d坐标绘制热图?
If you need to iterate over a meshgrid, try this: 如果需要遍历meshgrid,请尝试以下方法:
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])
I don't know if matplotlib is going to have easy ploting for heatmaps from generic 3-d points. 我不知道matplotlib是否可以轻松绘制来自通用3-d点的热图。 It would have to handle the generic case of scattered, out-of-order and missing points. 它必须处理散乱,无序和缺失点的一般情况。
I came up with this solution using DrRobotNinja approach 我使用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])
I superimpose heatmap and contours (levels) 我叠加热图和轮廓(水平)
#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)
and display 和显示
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.