Why the following python code outputs blank contour plot?

I'm getting the blank plot by using the below code and data file. Could you please let me know what's wrong with data file or the code?

import numpy as np
import matplotlib.pyplot as plt                                                                                                                                  
data = np.genfromtxt('file1.txt', delimiter=' ')                                                                                                                                                             
lats =  data[:,0]                                                                                                                                                                      
lons =  data[:,1]                                                                                                                                                                     values = data[:,2]                                                                                                                                                                      
lat_uniq, lat_idx = np.unique(lats, return_inverse=True)
lon_uniq, lon_idx = np.unique(lons, return_inverse=True)                                                                                                                           
xre, yre = np.meshgrid(lon_uniq, lat_uniq)
zre = np.full(xre.shape, np.nan)                                                                                                                                            
zre[lat_idx, lon_idx] = values
print(zre)
                                                                                                                                                                                                                                                                                            
fig, (ax1) = plt.subplots(1,1, figsize = (10, 5))
cp1 = ax1.contourf(xre, yre, zre, levels=4)
plt.colorbar(cp1, ax=ax1)
ax1.set_title("data are not interpolated")                                                                                                                                   plt.show()                                                                                                                                                               

test.txt file --

1 2 3

4 5 6

7 8 9

10 11 12

enter image description here


Solution 1:

Your program has no problem There were none values in your matrix, in which case the graph will not be drawn.

It makes no sense to have null values in a matrix and try to draw those points.

By changing its values, the 'contourf' command can fit the values of X, Y, and Z, and the graph is drawn.

See this :

import numpy as np
import matplotlib.pyplot as plt

data = np.genfromtxt('test.txt', delimiter=' ')
lats = data[:, 0]
lons = data[:, 1]
values = data[:, 2]
lat_uniq, lat_idx = np.unique(lats, return_inverse=True)
lon_uniq, lon_idx = np.unique(lons, return_inverse=True)
xre, yre = np.meshgrid(lon_uniq, lat_uniq)
# zre = np.full(xre.shape, np.nan)
zre = np.full(xre.shape, 0)
zre[lat_idx, lon_idx] = values
print(zre)

fig, (ax1) = plt.subplots(1, 1, figsize=(10, 5))
cp1 = ax1.contourf(xre, yre, zre, levels=4)
plt.colorbar(cp1, ax=ax1)
ax1.set_title("data are not interpolated")

plt.show()

Output:

enter image description here