ValueError: cannot reshape array - contour plot python

Matplotlib expects a contour plot to receive data in a specific format. Your approach does not provide the data in this format; you have to transform your data like this:

import numpy as np
import matplotlib.pyplot as plt
#from matplotlib.colors import LogNorm
data = np.genfromtxt('test.txt', delimiter=' ')
#print(data)
lats =  data[:,0]
## lon => x                                                                                                                                                               
lons =  data[:,1]
## values => z                                                                                                                                                            
values = data[:,2]
###     
#get unique lat lon values and their index positions                                                                                                                                                                 
lat_uniq, lat_idx = np.unique(lats, return_inverse=True)
lon_uniq, lon_idx = np.unique(lons, return_inverse=True)

#create 2D array necessary for the contour plot
xre, yre = np.meshgrid(lon_uniq, lat_uniq)
zre = np.full(xre.shape, np.nan)
#or if you know the standard value of the array, fill it with that
#zre = np.full(xre.shape, 0)
zre[lat_idx, lon_idx] = values
print(zre)

#you can fill in missing data with interpolation
from scipy.interpolate import griddata 
zre_interpolated = griddata((lons, lats), values, (xre, yre), method = "linear")
print(zre_interpolated)

#### later in the defined map                                                                                                                                             
fig, (ax1, ax2) = plt.subplots(1,2, figsize = (10, 5))

cp1 = ax1.contourf(xre, yre, zre, levels=4)
plt.colorbar(cp1, ax=ax1)
ax1.set_title("data are not interpolated")

cp2 = ax2.contourf(xre, yre, zre_interpolated, levels=4)
plt.colorbar(cp2, ax=ax2)
ax2.set_title("interpolated data")

plt.show()

Example output: enter image description here

The example output was generated using the following data in the txt file:

1 1 1
1 2 2
2 4 9
4 5 2
6 1 1
6 2 8
6 4 9
6 5 2
2 5 3
4 2 5
4 3 8
4 4 5 
1 3 4
1 5 2
2 1 1
2 3 4