Creating a heat map using 3D data in matplotlib

I have a function returnValuesAtTimethat returns three lists - x_vals, y_valsand swe_vals. All three lists are the same length, and each element in swe_valscorresponds to a x-valueof x_valsand a y-valueof y_vals. I want to create a heatmap with a legend that uses coordinates (x,y)and swe_valsas intensity.

I wrote the following code:

def plotValuesAtTimeMap(t):
    x_vals,y_vals,swe_vals=returnValuesAtTime(t)
    x_points=len(x_vals)
    y_points=len(y_vals)
    xx=np.linspace(x_vals[0],x_vals[-1],x_points)
    yy=np.linspace(y_vals[0],y_vals[-1],y_points)
    fig,ax=plt.subplots()
    im=ax.pcolormesh(xx,yy,z)
    fig.colorbar(im)
    ax.axis('tight')
    plt.show()

As soon as the three lists are obtained using returnValuesAtTime(t), I will take the length x_valsand y_vals. Then I use them to generate an interval for x and y directions, with the limits being the first and last elements of x_valsand y_vals. Then I try to create colormesh. But it gives me an empty colormeshwithout meaning.

What could be wrong? Will using a 3D array numpyinstead of three lists solve the problem?

The first 10 elements of each of the lists:

x_vals[0:10]

[439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893]

y_vals[0:10]

[4514018.2797159087,
 4513898.2797159087,
 4513778.2797159087,
 4513658.2797159087,
 4513538.2797159087,
 4513418.2797159087,
 4513298.2797159087,
 4513178.2797159087,
 4513058.2797159087,
 4512938.2797159087]

swe_vals[0:10]

[2.7520323,
 2.7456229,
 2.7456021,
 2.745455,
 2.7478349,
 2.7445269,
 2.7484877,
 2.7524617,
 2.75491,
 2.7509627]

Edit

I have added a scatter plot showing the grid range (x, y) below:

enter image description here

x y , 6804. (x, y) z- , 6804. , , , z, . - , :

enter image description here

z . , .

( CT Zhu):

enter image description here

+4
1

, x, y, z , contourf:

In [7]:X
Out[7]:
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

In [8]:Y
Out[8]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
       [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])

plt.contourf(X,Y,np.random.random((10,10))) #reshape Z too!
plt.colorbar()

enter image description here

+5

Source: https://habr.com/ru/post/1535091/


All Articles