Python heatmap from 3d coordinates

I start with two linspaces and I have them meshgrid. Then I calculate the function values ​​on the grid. My function is called cpt_hcpv() . Then I would like to combine my data with each grid point with the corresponding function value.

The 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)] 

from

 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 don’t calculate cpt_hcpv() directly on the grid: because I have to deal with the condition in understanding truncated = [(p, s) for p, s in zip(poro, sw) if p > pCut and s < sCut ] , so that I have to iterate over a point in the grid. I don't know how to iterate on a meshgrid.

So, I would like to warm up from 3d coordinates : in points I have x and y for points and in hcpv I have z parameters for each point, in the same order.

From the examples I found, there are pylab and matplotlib solutions for constructing a heat dissipation diagram from the values ​​of meshgrid + calculated on the grid, using a method that takes meshgrid as an argument.

Is there a way to build a heat map from 3d coordinates?

+4
source share
2 answers

I came up with this solution using the DrRobotNinja approach

 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]) 

Impose a 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() 
0
source

If you need to iterate over a meshgrid, try the following:

 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 do not know if matplotlib will have an easy layout for heatmaps from common 3 points. He would have to handle the general case of scattered, non-standard and missing points.

+1
source

Source: https://habr.com/ru/post/1498994/


All Articles