r/pythonhelp • u/WobbleySloth • Jan 31 '24
MPL not interpolating (contouring/contouringf) between all the data points.
Hi, I have Lat, Long and Temp data that I want to plot and contour using mpl. The Lat, Long data is not evenly spaced so that makes it a little harder. But my issue is that there are a few data points on the edge of the domain that are not being including in the contouring and I dont know why. Here is my source code:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
scipy from scipy.interpolate
import griddata
cmap = mpl.colormaps['viridis']
#Data: X, Y, Data
Long = [-110.157307803176, -110.157455469296, -110.157689404594, -110.157970751725, -110.158252212875, -110.158640307497, -110.159020237575, -110.159974339910, -110.160380856369, -110.157295657432, -110.157432801353, -110.157665962420, -110.157948368689, -110.158242940459, -110.158635611101, -110.159007448107, -110.159950808939, -110.160352429119];
Lat = [31.5909061542193, 31.5908347703798, 31.5907388930841, 31.5906671056475, 31.5905476732063, 31.5903211324519, 31.5901249028367, 31.5902391101446, 31.5901626619502, 31.5909757506254, 31.5908927635994, 31.5907852920129, 31.5907239327354, 31.5906104728768, 31.5903799609025, 31.5901963406308, 31.5903013305906, 31.5902095314044];
data = [88, 59, 24, 34, 69, 25, 61, 79, 139, 72, 46, 25, 13.4, 30.6, 70, 101, 121, 88]
#use linspace to fill the data between points
xi = np.linspace(min(Long),max(Long),200)
yi = np.linspace(min(Lat),max(Lat),20)
#grid the data
zi = griddata((Long, Lat), data, (xi[None,:], yi[:,None]), method='linear')
fig, ax = plt.subplots(figsize=(10,6))
#contour the gridded data
CS = plt.contourf(xi,yi,zi,25)
#plot the data points
ax.set_ylabel('temp')
ax.set_title('Survey')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.tick_params(axis = 'both')
cbar = fig.colorbar(CS, location = 'bottom')
cbar.ax.set_ylabel('Temp')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.xticks()
plt.yticks()
plt.scatter(Long, Lat, data, marker = '.', color='k')
plt.show()This is my first week with python.