Visualizing 3d Triangles With Pure Matplotlib Function

The problem: STL file is read, you want to do a quick sanity check if it’s correct and don’t have access to VTK or third party STL viewer.

Solution: Use matplotlib to visualize the STL.

Here’s a simple matplotlib script to visualize the STL triangles.


# coding: utf-8
import mpl_toolkits.mplot3d as a3d
import matplotlib.colors as colors
import matplotlib.pyplot as plt
import numpy as np

fname=r'coordtopo.npy'

coords,topo=np.load(fname)
ax = a3d.Axes3D(plt.figure())

xm,ym,zm=coords.max(axis=0)
xmi,ymi,zmi =coords.min(axis=0)

for nodes in topo:
tri = a3d.art3d.Poly3DCollection([coords[nodes,:3]])
tri.set_color(colors.rgb2hex([0.9,0.6,0.]))
tri.set_edgecolor('k')
ax.add_collection3d(tri)

ax.set_xlim3d([xmi,xm])
ax.set_ylim3d([ymi,ym])
ax.set_zlim3d([zmi,zm])

plt.show()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s