我有三个对应于xyz坐标空间的3D网格矩阵(X,Y,Z).
我还有一个3D Numpy矩阵A,其中A [i,j,k]包含一个与点(x,y,z)相关联的浮点数,其中x = X [i,j,k],y = Y [i ,j,k]和z = Z [i,j,k].浮点值在A内是连续的(即A的相邻元素之间的值的变化通常很小).
有没有办法使用Matplotlib或任何其他基于Python的图形包绘制对应于A中给定浮点值的曲面?例如,如果给定值2.34,我有兴趣获得矩阵A的绘制轮廓曲面,只要2.34(加或减一些公差)出现?
到目前为止,我已经能够恢复A中所有值的xyz坐标,这些坐标在目标值的某个容差范围内,然后使用this(下面的代码)制作3D散点图.也许还有一种方法可以从这些点绘制表面?
def clean (A, t, dt):
# function for making A binary for t+-dt
# t is the target value I want in the matrix A with tolerance dt
new_A = np.copy(A)
new_A[np.logical_and(new_A > t-dt, new_A < t+dt)] = -1
new_A[new_A != -1] = 0
new_A[new_A == -1] = 1
return (new_A)
def get_surface (X, Y, Z, new_A):
x_vals = []
y_vals = []
z_vals = []
# Retrieve (x,y,z) coordinates of surface
for i in range(new_A.shape[0]):
for j in range(new_A.shape[1]):
for k in range(new_A.shape[2]):
if new_A[i,j,k] == 1.0:
x_vals.append(X[i,j,k])
y_vals.append(Y[i,j,k])
z_vals.append(Z[i,j,k])
return (np.array(x_vals), np.array(y_vals), np.array(z_vals))
cleaned_A = clean (A, t=2.5, dt=0.001)
x_f, y_f, z_f = get_surface (X, Y, Z, cleaned_A )
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d', aspect='equal')
ax.scatter(x_f, y_f, z_f, color='g', s=1)
我也尝试过ax.plot_trisurf(x_f,y_f,z_f),但这给了我一个连接不佳的情节.我猜测我的数组中的值的排序可能会影响这个,在这种情况下,有一个包可以做点某些3D插值表面绘图与点的随机排序(例如通过最小化表面积或类似的东西?)
我感兴趣的对象是大致球形(即每个(x,y)两个z).我似乎无法找到有人在一个封闭的3D表面上进行三角测量的任何工作示例,但也许我没有找到正确的位置.
最佳答案 经过大量的挖掘,我认为我已经找到了一个有效的解决方案(至少在一个领域 – 当我尝试球体的变形时会更新我的答案).非常感谢有助于我思考正确道路的评论.我基本上使用ConvexHull从scipy.spatial进行三角测量:
from matplotlib.tri import Triangulation
from scipy.spatial import ConvexHull
def clean (A, t, dt):
# function for making A binary for t+-dt
# t is the target value I want in the matrix A with tolerance dt
new_A = np.copy(A)
new_A[np.logical_and(new_A > t-dt, new_A < t+dt)] = -1
new_A[new_A != -1] = 0
new_A[new_A == -1] = 1
return (new_A)
def get_surface (X, Y, Z, new_A):
x_vals = []
y_vals = []
z_vals = []
# Retrieve (x,y,z) coordinates of surface
for i in range(new_A.shape[0]):
for j in range(new_A.shape[1]):
for k in range(new_A.shape[2]):
if new_A[i,j,k] == 1.0:
x_vals.append(X[i,j,k])
y_vals.append(Y[i,j,k])
z_vals.append(Z[i,j,k])
return (np.array(x_vals), np.array(y_vals), np.array(z_vals))
cleaned_A = clean (A, t=2.5, dt=0.001)
x_f, y_f, z_f = get_surface (X, Y, Z, cleaned_A )
Xs = np.vstack((x_f, y_f, z_f)).T
hull = ConvexHull(Xs)
x, y, z = Xs.T
tri = Triangulation(x, y, triangles=hull.simplices)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d', aspect='equal')
ax.plot_trisurf(tri, z, color='g', alpha=0.1)