简体   繁体   English

如何用 Python 改进这个插值结果?

[英]How to improve this interpolation result with Python?

I have this XYZ text file (3 columns) and I'm trying to run an interpolation, so I can create a nice color model.我有这个 XYZ 文本文件(3 列)并且我正在尝试运行插值,因此我可以创建一个漂亮的颜色模型。 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:我得到的: 在此处输入图片说明 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()```

Got it to work following this question: Limit/mask matplotlib contour to data area按照这个问题让它工作: Limit/mask matplotlib contour to data area

The result:结果:

在此处输入图片说明

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM