如何从 3d 阵列(体积)中提取表面

问题描述 投票:0回答:1

我像这样创建一个 3d 数组:

arr = np.zeros((100, 100))
for i in range(5):
    for j in range(5):
        arr[i*20:(i+1)*20, j*20:(j+1)*20] = i+1
arr = np.tile(arr[np.newaxis,:,:], (100,1,1))
arr = np.transpose(arr, (0, 2, 1))

生成的形状将是

(100,100,100)
。在
y=50
:

会是这个样子
fig = plt.figure(figsize=(5, 5))
y=50
x, z = np.arange(arr.shape[0]), np.arange(arr.shape[2])
xv, zv = np.meshgrid(x,z)
plt.pcolormesh(xv, zv, arr[:,y,:].T, cmap='plasma')
plt.colorbar()
plt.xlabel('x')
plt.ylabel('z')
plt.title('3d array - y:{}'.format(y))
plt.gca().invert_yaxis()
plt.show()

现在我想创建这些区域之间的界面,在这个数组中有 5 个区域,由值(1、2、3、4 和 5)确定,因此生成的界面将为 4,如何估计它们和存储在另一个数组中,以便稍后将它们绘制为表面,如 this?

python numpy numpy-ndarray
1个回答
0
投票
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import PolyCollection

# Delete all of your existing code and replace with the equivalent
arr = np.tile(np.arange(1, 6).repeat(20), (100, 100, 1))

# ...but delete that, too. Your "interface" planes are trivially defined via:
verts = np.zeros((4, 4, 2))
verts[:, 2:, 0] = 100
verts[:, 1:3, 1] = 100

fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
facecolors = plt.colormaps['plasma'](np.linspace(0, 1, len(verts)))
poly = PolyCollection(verts, facecolors=facecolors, alpha=.7)
ax.add_collection3d(poly, zs=np.arange(20, 100, 20), zdir='y')
ax.set(xlim=(0, 100), xlabel='x',
       ylim=(0, 100), ylabel='z',
       zlim=(0, 100), zlabel='y')

plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.