问题描述
如何通过逆距离加权对数据进行插值?
当我使用scipy.griddata
时,90度仰角周围的点不合理
有什么解决办法吗?
谢谢。
解决方法
您可以尝试使用 plotly
绘制它,它为您提供交互式绘图。您可以验证 terrain
中每个点的值。
您可以参考以下代码了解详情。
import plotly.graph_objects as go
import numpy as np
import pandas as pd
# Read data from a csv
url = 'https://raw.githubusercontent.com/patel999jay/Bedford_python/master/bathy_bedford.csv'
z_data = pd.read_csv(url)
# z_data.head() # to see dataframe head
# Read data from a csv
url = 'https://raw.githubusercontent.com/patel999jay/Bedford_python/master/bathy_bedford.csv'
z_data = pd.read_csv(url,index_col=0)
z = z_data.values
sh_0,sh_1 = z.shape
x,y = np.linspace(44.66875,44.74791667,sh_0),np.linspace(-63.69791667,-63.52708333,sh_1) # This is lat and long
fig = go.Figure(data=[go.Surface(z=z,x=x,y=y,colorscale='Viridis')])
fig.update_traces(contours_z=dict(show=True,usecolormap=True,highlightcolor="limegreen",project_z=True))
fig.update_layout(title='Bedford Basin Elevation',xaxis_title="Latitude",yaxis_title="Longitude",autosize=False,width=900,height=900,margin=dict(l=65,r=50,b=65,t=90))
fig.update_layout(scene = dict(
xaxis_title='Latitude',yaxis_title='Longitude',zaxis_title='Elevation'),margin=dict(r=20,b=10,l=10,t=10))
fig.update_layout(coloraxis_colorbar=dict(
title="Elevation",thicknessmode="pixels",thickness=50,lenmode="pixels",len=200,yanchor="top",y=1,ticks="outside",ticksuffix="",dtick=5
))
fig.show()
如果你想使用 matplotlib
那么你可以参考下面的代码。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.gridspec as gridspec
import matplotlib as mpl
# from matplotlib.ticker import LinearLocator,FormatStrFormatter
from matplotlib import rc,rcParams
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
import pandas as pd
url ="https://raw.githubusercontent.com/patel999jay/Bedford_python/master/POINT_DATA_TITLE.csv"
df = pd.read_csv(url)
# df.head() # see dataframe head
# 2D-arrays from DataFrame
x1 = np.linspace(df['x'].min(),df['x'].max(),len(df['x'].unique()))
y1 = np.linspace(df['y'].min(),df['y'].max(),len(df['y'].unique()))
"""
x,y via meshgrid for vectorized evaluation of
2 scalar/vector fields over 2-D grids,given
one-dimensional coordinate arrays x1,x2,...,xn.
"""
x2,y2 = np.meshgrid(x1,y1)
# Interpolate unstructured D-dimensional data.
z2 = griddata((df['x'],df['y']),df['z'],(x2,y2),method='cubic')
# Ready to plot
fig = plt.figure(211,figsize=(15,20))
ax = fig.add_subplot(211,projection='3d')
spec = gridspec.GridSpec(ncols=1,nrows=2,height_ratios=[4,1])
surf = ax.plot_surface(x2,y2,z2,rstride=1,cstride=1,cmap=cm.terrain,linewidth=1,antialiased=False)
ax.view_init(25,-55)
cset = ax.contourf(x2,zdir='z2',offset=-5,antialiased=True)
rcParams['legend.fontsize'] = 20
rc('text',usetex=True)
rc('axes',linewidth=2)
rc('font',weight='bold')
ax.xaxis.set_tick_params(labelsize=20)
ax.yaxis.set_tick_params(labelsize=20)
ax.zaxis.set_tick_params(labelsize=20)
ax.set_zticks([-70,-50,-30,-10,10,30,50,70,90,110])
plt.title(r'\textbf{Bedford Basin Bathymatry}',fontsize=20)
plt.xlabel(r'\textbf{Latitude}',fontsize=20,labelpad= 23)
plt.ylabel(r'\textbf{Longitude}',labelpad= 20)
ax.set_zlabel(r'\textbf{Elevation}',labelpad= 10)
P.S. :您能否提供您的问题的最小可重现示例,然后我可以相应地更新我的答案。现在我使用的是随机数据。