简体   繁体   中英

Heatmap on x-y plane with z as weighting

I've gone through tens of answers regarding heatmaps on this forum but I am still running into problems, so I thought I'd ask myself. Bare in mind that until a month ago I had no idea what Python was.

So, I have a large file of data in three columns. The first two are standard xy coordinates. For each point, there is a third variable, z, that I want to use as weighting to build some sort of heatmap.

I have seen several methods, eg using meshgrid or changing array size, but what I think the problem is is that my array is not regular or rectangular. It's just a mess of random points in the xy plane, not evenly spaced with each other, each with az value.

Here is just a small snippet of the data I have in my spreadsheet:

x y z
392 616 0.5
416 614 1
497 603 3
533 598 3.5
383 589 0.5
574 574 4
...

I tried several methods, eg reshaping the arrays, but I always get some kind of error. How can I plot this data as a heatmap with the weighting of each point given by z? Thank you.

I'm aware that, since the data points are not regularly spaced out, there might be gaps where the heatmap would be zero, but I can sort those out later by extrapolating their weighting via a method I figured out, so that wouldn't be a problem.

The closest I got to getting the graph I'm looking for is using this code:

plt.hist2d(x, y, bins=8, weights=z, cmap="Greys")
plt.colorbar()

However, the problem with this is that, if there is more than one point in a given "bin", it computes the "aggregate" weighting -- eg if in a particular bin there are two data points with weightings of 1 and 2.5, respectively, the bin will be coloured as if its weighting was 1+2.5=3.5. Is there any way I can get it to display the colour corresponding to the weighting of the data point closest to the bin center?

eg if the data point with weighting 2.5 was really close to the bin center while the one with weighting 1 was along one of the bin's edges, is there a way I can get the bin to have weighting 2.5?

Thank you and sorry for disturbing.

Have a look at http://scipy-cookbook.readthedocs.io/items/Matplotlib_Gridding_irregularly_spaced_data.html

The idea is to use griddata from scipy.interpolate to get your irregularly spaced data on a regularly spaced grid.

If I assume you have your data x, y, z in numpy arrays, you can modify the example given in the docs:

# define grid.
xi = np.linspace(np.amin(x),np.amax(x),100)
yi = np.linspace(np.amin(y),np.amax(y),100)
# grid the data.
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')
# contour the gridded data
CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
plt.colorbar() # draw colorbar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM