来自3d坐标的Python热图
发布时间:2020-12-20 13:36:53 所属栏目:Python 来源:网络整理
导读:我从两个linspaces开始,我将它们网格化.然后我在网格上计算函数的值.我的函数叫做cpt_hcpv().然后我想热映射我的数据,网格上的每个点都有相应的函数值. 代码看起来像 poro = np.linspace(min(poro),max(poro))sw = np.linspace(min(sw),max(sw))g = np.meshg
我从两个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,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():因为我必须处理comprehension中的条件truncated = [(p,s)for p,sw)if p> pCut和s< sCut]所以我必须迭代网格中的点.我不知道如何迭代网格网格. 所以,我想从三维坐标中进行热图:在点上我有x和y表示点,而在hcpv中我有每个点的z参数,顺序相同. 从我发现的例子中,有一个pylab和matplotlib解决方案,用于绘制在网格上计算的网格网格值的热图,一个方法将meshgrid作为参数. 有没有办法从3d坐标绘制热图? 解决方法
如果需要遍历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,sw) for p,s in r] for r in g_arr]) 我不知道matplotlib是否可以轻松绘制来自通用3-d点的热图.它必须处理散乱,无序和缺失点的一般情况. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |