跨网格三维Matplotlib曲面图的插值保持对角线一致性

时间:2017-07-26 18:56:00

标签: python numpy matplotlib scipy

我正在尝试使用少量数据点来更容易地查看3D曲面图。我这样做是通过在每个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

0 个答案:

没有答案