1

I have this XYZ text file (3 columns) and I'm trying to run an interpolation, so I can create a nice color model. The problem is that the returned result is not correctly representing the topography of my data distribution... I mean, the surface it is not as smooth as I want it to be. The bottom colors (red) are following the data (black dots), but the top colors (blue) are not. Any help?

What I get: enter image description here My code:

import matplotlib.pyplot as plt
import scipy.interpolate

x,y,z = np.loadtxt("vel_model.txt",usecols=(0,1,2),unpack=True)

x_grid = np.linspace(x.min(),x.max(),100)
y_grid = np.linspace(y.min(),y.max(),100)
xi,yi = np.meshgrid(x_grid,y_grid)
zi = scipy.interpolate.griddata((x, y), z, (xi, yi), method = 'cubic')
fig = plt.figure()
ax = fig.add_subplot(111)
cm = ax.contourf(xi, yi, zi, cmap='jet')
ax.scatter(x,y,color='black',s = 6, label = 'Data points')
ax.set_aspect('equal')
ax.set_xlabel('Distance (m)')
ax.set_ylabel('Elevation (m)')
plt.legend(loc = 'best')
plt.grid(color='grey', linestyle='--', linewidth=0.3)
plt.show()```
Victor José
  • 367
  • 1
  • 4
  • 12

1 Answers1

1

Got it to work following this question: Limit/mask matplotlib contour to data area

The result:

enter image description here

The code:

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
from matplotlib.path import Path
from matplotlib.patches import PathPatch

x,y,z = np.loadtxt("modeloVS_teste.txt",usecols=(0,1,2),unpack=True)
x_grid = np.linspace(x.min(),x.max(),100)
y_grid = np.linspace(y.min(),y.max(),100)
xi,yi = np.meshgrid(x_grid,y_grid)
zi = scipy.interpolate.griddata((x, y), z, (xi, yi), method = 'cubic')
fig = plt.figure()
ax = fig.add_subplot(111)
cm = ax.contourf(xi, yi, zi, cmap='jet')
ax.scatter(x,y,color = 'black',s = 5, label = 'Data points')
ax.set_aspect('equal')
ax.set_xlabel('Distance (m)')
ax.set_ylabel('Elevation (m)')
plt.legend(loc = 'best')
plt.grid(color='grey', linestyle='--', linewidth=0.3)
columns = {}
for i in range(len(x)):
    columns.update({x[i]:[]})
for i in range(len(x)):  
    columns[x[i]].append((y[i]))  
limits = []
for key in columns:
    limits.append((key,max(columns[key])))
limits = limits[::-1]
for key in columns:
    limits.append((key,min(columns[key])))
clippath = Path(limits)
patch = PathPatch(clippath, facecolor='none', alpha = 0)
ax.add_patch(patch)
for c in cm.collections:
    c.set_clip_path(patch)
plt.show()
Victor José
  • 367
  • 1
  • 4
  • 12