Plotting a heat map from three lists: X, Y, Intensity

后端 未结 4 1863
独厮守ぢ
独厮守ぢ 2020-12-15 10:07

I don\'t get how to create a heatmap (or contour plot) when I have x, y, intensity. I have a file which looks like this:

0,1,6
0,2,10
....

4条回答
  •  悲&欢浪女
    2020-12-15 11:04

    The index error arises from the fact that pcolormesh expects a 2D array while your arr is a 1D vector. Also if I understand correctly, your input file has the form

    0,1,z
    0,2,z
     ...
    0,ymax,z
     ...
    1,1,z
    1,2,z
     ...
    xmax,ymax,z
    

    In that case meshgrid(x,y) will not work as it expects something like meshgrid(range(xmax),range(ymax)) i.e. vectors without repeated values.

    In your case you need to find out how many distinct x and y values there are and then simply reshape your vectors into 2D arrays.

    shape = np.unique(x).shape[0],np.unique(y).shape[0]
    x_arr = x.reshape(shape)
    y_arr = y.reshape(shape)
    z_arr = z.reshape(shape)
    plt.pcolormesh(x_arr,y_arr,z_arr)
    

提交回复
热议问题