保持网格3D Matplotlib曲面P上插值的对角线一致性

2024-06-16 08:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用较少的数据点,使三维曲面图更容易看。我是通过在x和y数据点之间创建1000个线性连接点来实现的。这样做的缺点是,它只在正交方向上扩展数据,而在对角线轴上留下视差。你知道吗

你可以在这张图中看到,最高的两个点是对角的,它们之间有一个弯曲的倾角。然而,如果我有实际的数据点来表示它,我不会期望这种下降。有没有办法更准确地插值?你知道吗

import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
from matplotlib import cm
from scipy.interpolate import griddata


x = np.array([1200,  900,  600,  300, 1200,  900,  600,  300, 1200,  900,  600, 300])
y = np.array([32698675.77, 32698675.77, 32698675.77, 32698675.77, 22235099.52, 22235099.52, 22235099.52,  22235099.52, 11117549.76,  11117549.76,  11117549.76,  11117549.76])
z = np.array([ 157039.99991206, 112843.30660347, 86955.86661797, 110282.34660491, 99997.52952157, 211376.31395759, 126638.66680572,  88166.23539093, 246550.43164327, 127657.17661076,  84181.01970028, 111822.90208357])

xyz = {'x': x, 'y': y, 'z': z}
df = pd.DataFrame(xyz, index=range(len(xyz['x']))) 
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()))
x2, y2 = np.meshgrid(x1, y1)


xt = np.array([])
for mina in range(len(x1)):
    if mina < len(x1)-1:
            xn = np.linspace(x1[mina], x1[mina+1], 1000)
            xt = np.hstack((xt, xn))

yt = np.array([])
for mina in range(len(y1)):
    if mina < len(y1)-1:
            yn = np.linspace(y1[mina], y1[mina+1], 1000)
            yt = np.hstack((yt, yn))

x3, y3 = np.meshgrid(xt, yt)
z2 = griddata((df['x'], df['y']), df['z'], (x2, y2), method='cubic')
z3 = griddata((df['x'], df['y']), df['z'], (x3, y3), method='cubic')

fig = plt.figure()
ax = fig.add_subplot(3, 2, 1, projection='3d')
surf = ax.plot_surface(x2, y2, z2, cmap=cm.YlGnBu)
ax.set_ylabel('y')
ax.set_xlabel('x')
ax.set_zlabel('z')
ax.view_init(20, 100)
ax.yaxis.labelpad=20
ax.xaxis.labelpad=10
ax.zaxis.labelpad=20
ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
ax.ticklabel_format(style='sci', axis='z', scilimits=(0,0))
plt.ylim(np.max(y), np.min(y))
ax.set_xticks(np.unique(x))
ax.set_yticks(np.unique(y))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.tight_layout()

ax = fig.add_subplot(3, 2, 2, projection='3d')
surf = ax.plot_surface(x3, y3, z3, cmap=cm.YlGnBu)
ax.set_ylabel('y.')
ax.set_xlabel('x')
ax.set_zlabel('z')
ax.view_init(20, 100)
ax.yaxis.labelpad=20
ax.xaxis.labelpad=10
ax.zaxis.labelpad=20
ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
ax.ticklabel_format(style='sci', axis='z', scilimits=(0,0))
plt.ylim(np.max(y), np.min(y))
ax.set_xticks(np.unique(x))
ax.set_yticks(np.unique(y))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.tight_layout()

plt.show()

代码中有前后图的示例,如下图所示。 Surface Area Plot

SA Plot2


Tags: 数据importdflennpfigpltax