Heatmap complain NaN z values when there are actually valid z values

Hey all,
I am trying to plot a simple heatmap where I have x and y as 2D coordinate pairs, and z values on each of these points. You can see from my screen shot, I have 100% valid z values, but no plot is being generated and when I hover over my plotting domain, NaN z values are reported. I am really baffled by this and would really appreciate some help!

I have a sense of why this is not working now, and that is my data is not structured nicely. All my three x,y,z arrays are 1D vectors. When I plot x and y as a scatter plot, they look like the point cloud figure I show below (just the blue interior dots for now). So the z values are defined on each of these random-looking blue dots. I am not sure whether for this configuration it is still possible to plot a heatmap? I guess I would need some smoothing?

Click here to download my data.



Without inspecting your data we cannot help you.

In your Heatmap definition, type='heatmap' is superfluous, because go.Heatmap defines the trace type.

Thank you for the help. I was trying to upload a .npz data file, but the system here only accepts pictures. Do you know a way I can share my data file?

Also thanks for pointing out the type thing. I was just desperate to try anything that I can find online.



You can upload your file on your google drive or dropbox and set it to be shared with anyone having the link.
Then post the link here. Or if you have a github account you can post it there.

Thank you for the enlightenment.

I have edited my original post now with a link to the data. Also I realized what might be wrong with this, but I am still scratching my head trying to fix it.


A method to plot the heatmap from irregular data is to use griddata from scipy.interpolate:

import plotly.graph_objects as go
import numpy as np
from scipy.interpolate import griddata

x= -2+4*np.random.rand(200)
y= -2+4*np.random.rand(200)
z = y * np.exp(-x**2 - y**2)

grid_x, grid_y = np.mgrid[-2:2:100j, -2:2:200j]

Z = griddata((x, y), z, (grid_x, grid_y) , method='linear')

fig=go.Figure(go.Heatmap(y=grid_x[:,0], x=grid_y[0,:], z=Z, colorscale='Viridis'))


Your data however are too dense and except a few positions z-data are almost constant:

pcloud = np.load('pcloud2d_z.npz')

x, y, z = pcloud['x'],  pcloud['y'], pcloud['z']
xm, xM = x.min(), x.max()
ym, yM = y.min(), y.max()
print(xm, xM, ym, yM)

grid_x, grid_y = np.mgrid[xm:xM:1000j, ym:yM:1000j]
Z = griddata((x,y), z, (grid_x, grid_y), method='linear')

fig=go.Figure(go.Heatmap(y=grid_x[:, 0], x=grid_y[0,:], z=Z, colorscale='Viridis'))