Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to export an stl file from a matplotlib surface plot?

I am analyzing the shape changes of a region of the developing heart in mouse embryos. For that, I study how the spatial coordinates of cells within that tissue change over time.

I am using the function matplotlib.plot_trisurf to obtain the triangulated surface between the coordinates of these cells:

tissue surface

Now I want to calculate the area of this triangulated surface and extract the surface as a .stl file so I can use in other 3D browsers like MeshLab. I don't know how to do it.

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) 

I am looking for something like this:

surface.save('mysurface.stl', ax.plot_trisurf(X,Y,Z))
surface.calculate_area(ax.plot_trisurf(X,Y,Z))
like image 268
Miquel Sendra Avatar asked Oct 26 '25 10:10

Miquel Sendra


2 Answers

I found this solution on google, it worked for me. In my case I did the triangulation before plotting. Install the library numpy-stl (https://github.com/WoLpH/numpy-stl) before. Then, try to add the following code:

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')
like image 64
Rafael Furquim Avatar answered Oct 28 '25 23:10

Rafael Furquim


For some reason I was unable to add comment but I found my code working perfectly fine based off of Rafael Furquim's response In my case I wanted a 2d plot to be exported in .stl:

import stl # from numpy-stl
from stl import mesh
import numpy as np
import matplotlib.tri as mtri
triang=mtri.Triangulation(x2, y2)
data = np.zeros(len(triang.triangles), dtype=mesh.Mesh.dtype)
mobius_mesh = mesh.Mesh(data, remove_empty_areas=False)
mobius_mesh.x[:] = x2[triang.triangles]
mobius_mesh.y[:] = y2[triang.triangles]
mobius_mesh.z[:] = z[triang.triangles]
mobius_mesh.save('mysurface.stl')
like image 38
Sree Avatar answered Oct 28 '25 23:10

Sree



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!