-1

I'm wondering if there is a way to use matplotlib and numpy to plot the heatmap of three lists. My grid is not regular, it is oddly shaped so this does not work for me: Plotting a Heat Map X,Y,Intensity From Three Lists. When I try that, I get ValueError: cannot reshape array of size 1906 into shape (1847,127). My lists have multiple of the same X and Y values, and are not in any way rectangular. I was wondering if there is a way to plot my data so it looks like the meshgrid/imshow grids that you can get when you have rectangular data. Basically, I just want to plot a bunch of given intensities at given X, Y values and have them show as a heatmap.

Thanks!

edit: Here is the kind of data that I am working with. I'm trying to use the 4th column as the intensity and the 1st and 2nd columns as the x and y respectively. https://pastebin.com/XCcwRiJn

  • 1
    Could you share some "sample" + expected outcome? Also If you have same X and Y values, how should the Zs combined for these? – MSeifert May 31 '17 at 12:39
  • You may try a1 2D interpolation of your data. Check out: https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.interpolate.griddata.html – ymmx May 31 '17 at 12:48
  • Added example data. – bigkittycat May 31 '17 at 12:50
  • Still unclear what the expected outcome is. I already used my close vote, but someone may also mark this as duplicate of e.g. [this question](https://stackoverflow.com/questions/42494642/python-problems-contour-plotting-offset-grid-of-data) or [this question](https://stackoverflow.com/questions/42702920/plotting-isolines-contours-in-matplotlib-from-x-y-z-data-set). – ImportanceOfBeingErnest May 31 '17 at 13:02
  • [This one](https://stackoverflow.com/questions/9008370/python-2d-contour-plot-from-3-lists-x-y-and-rho) might also be a possible duplicate – ImportanceOfBeingErnest May 31 '17 at 13:05
  • And [this one](https://stackoverflow.com/questions/3864899/resampling-irregularly-spaced-data-to-a-regular-grid-in-python) might be of help. – ImportanceOfBeingErnest May 31 '17 at 13:07

1 Answers1

0

Thanks ymmx, I'm new to Stack Overflow so I don't know how to turn your answer into the actual answer. Here's what I did to make it work using a 2D iterpolation:

myData = np.genfromtxt(fileName, delimiter=",")
X = myData[:, 0]
Y = myData[:, 1]
intensity = myData[:, 3]
XY = np.column_stack((Y,X))
grid_x, grid_y = np.mgrid[-.2:.2:100j, 0:.05:200j]
grid1 = griddata(XY, intensity, (grid_x, grid_y), method='cubic')
plt.imshow(grid1.T, extent=(0, .5, 0, .05), origin='lower',cmap='jet')

If someone has this same problem, you have to adjust your mgrid and extent values to fit your data.

Thanks for the help!