Heatmap on x-y plane with z as weighting

martin

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 x-y 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, e.g. 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 x-y plane, not evenly spaced with each other, each with a z 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, e.g. 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 -- e.g. 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?

e.g. 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.

datasailor

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Map<X, Map<Y, Z> to Map<Y, Map<X, Z>

x <*> y <$> z in Haskell

X Y Z array data to heatmap

Plotly Python - Heatmap - Change Hovertext (x,y,z)

Remove lines in surface plot in plotly to x-, y-, and z-plane

set x and y range of plotly heatmap

Plotting x and y values with result as color / heatmap

Calculate x z movement on plane with camera rotation

Trigonometry accelerometer Z angle from gravity vector (normal vector to X Y plane)

Inverse Distance Weighting : Error in spDists(s0, s) : ncol(x) == ncol(y) is not TRUE in package gstat

How to convert x y z points into x y z spheres

Make a heatmap of x,y,z data in Python

Heatmap with just x (or y) axis

Generate a heatmap in a set X, Y, Z with Z being the intensity of the color

Simple heatmap with 2 1D columns as x and y-axis, and 1 1D column as z-axis

Using gluUnProject to map touches to x,y cords on z=0 plane in Android OpenGL ES 2.0

Create heatmap with (x,y) coordinated order

Perspective transform given point (x,y) in quadrilateral plane to Rectangle plane's point (x', y')?

Calculate Y of 3D point on 3D QUAD PLANE based on X,Z in that point

template functions for points with x,y,z and points with x(), y(), z()

Matplotlib: data not showing on the two sides in heatmap using x, y, z values stored in lists

Stretching a line from a point in the xy-plane towards (x,y,z) point in matplotlib

why is the pixel coordinate in x,y plane x + y*width?

Using gnuplot, how can I plot sin(x) in the x-z plane and sin(x) in the x-y plane, both at the same time?

How to rotate a 3D matrix along X,Y, or Z axial plane?

How to proof (x+y)(x’+z)(y+z) = (x+y)(x’+z)

Plot a 2D Colormap/Heatmap in matplotlib with x y z data from a pandas dataframe

how to create an arc path from 3 points(x, y, z) in plane?

Producing a heatmap from a pandas dataframe with rows of the form (x,y,z), where z is intended to be the heat value