python - 有没有办法从 matplotlib 曲面图导出 STL 文件?

标签 python matplotlib surface

我正在分析小鼠胚胎中正在发育的心脏区域的形状变化。为此,我研究了该组织内细胞的空间坐标如何随时间变化。

我正在使用函数 matplotlib.plot_trisurf 获取这些单元格坐标之间的三角面:

tissue surface

现在我想计算这个三角形表面的面积并将表面提取为 .STL 文件,这样我就可以在其他 3D 浏览器(如 MeshLab)中使用。我不知道该怎么做。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

##Data, spatial coordinates for each of the cells forming the tissue.

X = np.array([157.68627725, 103.11761198, 157.51873667, 165.81563644,
        77.9816598 , 126.77531671,  90.24263806, 162.27734804,
       122.62725478,  83.2503042 , 162.59702484,  88.3921336 ])

Y = np.array([-174.21831735, -144.38094418, -144.87819434, -181.84569162,
       -116.19711147, -133.53935007, -139.02422794, -141.49550572,
       -137.70714927, -144.36804192, -174.05904052, -120.61181162])

Z = np.array([-40.21972608,  19.97958051, -49.49361177, -69.6049367 ,
       -10.5853926 , -22.06279801,  23.51722221, -72.24126518,
         8.24172533,  42.0251029 , -49.31600354, -16.93026202])

##Plot triangulated surface

fig = plt.figure(figsize = (10, 4.5))
ax = fig.add_subplot(121, projection = '3d')

ax.plot_trisurf(X,Y,Z) 

我正在寻找这样的东西:

surface.save('mysurface.stl', ax.plot_trisurf(X,Y,Z))
surface.calculate_area(ax.plot_trisurf(X,Y,Z))

最佳答案

我在谷歌上找到了这个解决方案,它对我有用。就我而言,我在绘图之前进行了三角测量。之前安装库 numpy-STL ( https://github.com/WoLpH/numpy-stl )。然后,尝试添加以下代码:

import stl
from stl import mesh
triang=triangulation(x,y,z)
data = np.zeros(len(triang.triangles), dtype=mesh.Mesh.dtype)
mobius_mesh = mesh.Mesh(data, remove_empty_areas=False)
mobius_mesh.x[:] = x[triang.triangles]
mobius_mesh.y[:] = y[triang.triangles]
mobius_mesh.z[:] = z[triang.triangles]
mobius_mesh.save('mysurface.stl')

关于python - 有没有办法从 matplotlib 曲面图导出 STL 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56545819/

相关文章:

python - 在 cygwin 中安装 matplotlib 时出错

android - 用于旧版 Android 设备的 ARCore

python - Cygwin 上的 os.name 是什么?

python - 如何在 Heroku 中使用 OpenCV

python - 对于 python,安装 hdf5/netcdf4

c++ - SDL2 纹理操作教程无法执行 - Lazy Foo http ://lazyfoo.net/tutorials/SDL/40_texture_manipulation/index.php

opengl - CAD中C2曲面是什么,C2是什么意思?

python - Py2App错误: ModuleNotFoundError: No module named 'cmath' when using Pandas

python - 保存交互式 Matplotlib 图形

matplotlib - 如何让 iPython inbuild 魔术命令在 Jupyter notebook Pyspark 内核中工作?