如何使用matplotlib / numpy在Python中给出三个不规则的值列表来绘制热图

时间:2017-05-31 12:38:47

标签: python numpy matplotlib

我想知道是否有办法使用matplotlib和numpy来绘制三个列表的热图。我的网格不规则,形状奇特,所以这对我不起作用:Plotting a Heat Map X,Y,Intensity From Three Lists。当我尝试时,我得到ValueError: cannot reshape array of size 1906 into shape (1847,127)。我的列表有多个相同的X和Y值,并且不以任何方式呈矩形。我想知道是否有一种方法来绘制我的数据,所以它看起来像你有矩形数据时可以获得的meshgrid / imshow网格。基本上,我只想在给定的X,Y值上绘制一堆给定的强度,并将它们显示为热图。

谢谢!

编辑:这是我正在使用的数据类型。我试图使用第4列作为强度,第1列和第2列分别作为x和y。 https://pastebin.com/XCcwRiJn

1 个答案:

答案 0 :(得分: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!