How to display a 3D plot of a 3D array isosurface with mplot3D or similar

Just to elaborate on my comment above, matplotlib’s 3D plotting really isn’t intended for something as complex as isosurfaces. It’s meant to produce nice, publication-quality vector output for really simple 3D plots. It can’t handle complex 3D polygons, so even if implemented marching cubes yourself to create the isosurface, it wouldn’t render it properly.

However, what you can do instead is use mayavi (it’s mlab API is a bit more convenient than directly using mayavi), which uses VTK to process and visualize multi-dimensional data.

As a quick example (modified from one of the mayavi gallery examples):

import numpy as np
from enthought.mayavi import mlab

x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)

src = mlab.pipeline.scalar_field(s)
mlab.pipeline.iso_surface(src, contours=[s.min()+0.1*s.ptp(), ], opacity=0.3)
mlab.pipeline.iso_surface(src, contours=[s.max()-0.1*s.ptp(), ],)

mlab.show()

enter image description here

Leave a Comment