Rendering your STL files with matplotlib using numpy-stl

It’s been a while since the first version of numpy-stl and a lot has changed since.

Most importantly, usage has become even easier. So here’s a demo on how to use matplotlib to render your stl files:

from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot

# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)

# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file('tests/stl_binary/HalfDonut.stl')

# Auto scale to the mesh size
scale = your_mesh.points.flatten(-1)
axes.auto_scale_xyz(scale, scale, scale)

# Show the plot to the screen

You can make the render prettier yourself of course, but it is certainly useful for testing.

Screen Shot 2015-07-10 at 18.53.28

Bookmark and Share


About Rick van Hattem

Rick van Hattem is a Dutch Internet entrepreneur and co-founder of

6 Responses to “Rendering your STL files with matplotlib using numpy-stl”

  1. Luis | 2016-09-09 at 17:00:49 #

    Hi Rick,

    I have a question regarding rendering a stl file with python, do you think is possible to measure distance on an irregular stl model captured with a 3d scanner?

    I look forward for your answer

    Kind regards

    • Rick van Hattem | 2016-09-09 at 17:53:24 #

      Hi Luis,

      Technically it’s not impossible of course but it’s definitely not that easy to do right. You need to filter out incorrect data and fill missing information which makes it very difficult. It’s definitely outside of the numpy-stl scope for the time being but you could easily use numpy-stl to do the processing using numpy and matplotlib.

      Best, Rick

      • Luis | 2016-09-11 at 15:44:59 #

        Hi Rick,

        Thanks a lot for your answer, but this is what I’m trying to do, to specify, I have a stl model of a welding bead, which I obtained from a 3d scanner and I would like to obtain its dimension (height and widht of the bead) . I was thinking of cutting many cross section measure this sections get their values and then obtain the mean and the standard deviation, but I don’t have any clue of how can I do this in Python, so any little help is welcome.

        I look forward for your answer
        Thank you


        • Rick van Hattem | 2016-09-14 at 18:08:20 #

          I think one of the easiest solutions would be to store all of the points in an kd-tree or octree. From that point on you can easily process the object bit by bit. Although instead of layers it will be clusters of points which are most likely more useful anyhow.

          It’s a bit outside of my expertise though and I have no idea if that’s the most convenient solution.

          A quick google gave me this overview, it could be helpful:

  2. SAGAR HUKKIRE | 2017-01-31 at 14:21:14 #

    I have two stl files of with two organs data of original MRI file . How I can merge to stl files to get the part I needed


    • Rick van Hattem | 2017-01-31 at 16:20:51 #

      The simple solution is to read both files and simply write it to a new file. Something along the lines of:

      import numpy
      from stl import mesh
      mesh_a = mesh.Mesh.from_file('file_a.stl')
      mesh_b = mesh.Mesh.from_file('file_b.stl')
      new_mesh = mesh.Mesh(numpy.concatenate([,]))

      Note that this will require you to have enough memory to keep both of the original models in your memory twice.

      This is definitely not the fastest solution since you’ll be parsing the data while copying it. A much faster solution would be to simply concatenate the files and merge the headers. That’s a bit outside of the scope of this project but it’s actually not that hard to do.

      For the binary stl format you simply need to add the sizes of both files to eachother (bytes 80 to 84) and concatenat bytes 84 till the end of both files to the new file.

      For the ascii format it’s pretty similar, you need to skip the “endsolid …” line for the first file and skip the “solid …” line for the second file and beyond that you can simply concatenate.

Leave a Reply