# How to plot output from marching_cubes_lewiner in python?

#### Tags: 3d, machine-learning, marching-cubes, python

I’ve been able to use the lewiner marching cubes algorithm in python. It outputs vertices, faces, and other attributes. I want to be sure that it is working correctly, so I’d like to plot a 3D image of what the function returns. However, I have not had any success so far. I have tried the following:

Successful retrieval of necessary fields:

```verts, faces, normals, values = skimage.measure.marching_cubes_lewiner(var,20,spacing=(0.5,0.5,0.5))
```

And unsuccessful plots of the retrieved values:

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

mesh = Poly3DCollection(verts[faces])
mesh.set_edgecolor('k')
ax.add_collection3d(mesh)
```

And also:

```vv.mesh(np.fliplr(verts), faces, normals, values) # doctest: +SKIP
```

Hypothetically, I would like to use the verts, faces, etc in a machine-learning algorithm, but I’d like to be sure that the returned values are reliable. Any one have experience with anything like this?

## Answer

I don’t know if you’re still looking for an answer to this, but I just experienced a problem for plotting the mesh through this function as well. I was getting no error but an empty plot. Should this be the case for you as well, I solved it by specifying the axis limits. The following did the trick for me:

```import matplotlib.pyplot as plt
import numpy as np
from skimage.measure import marching_cubes_lewiner
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

# mask is a currently stored binary 3D numpy array
verts, faces, normals, values = marching_cubes_lewiner(mask)
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection="3d")

ax.set_xlim(np.min(verts[:,0]), np.max(verts[:,0]))
ax.set_ylim(np.min(verts[:,1]), np.max(verts[:,1]))
ax.set_zlim(np.min(verts[:,2]), np.max(verts[:,2]))

mesh = Poly3DCollection(verts[faces])
mesh.set_edgecolor('k')
ax.add_collection3d(mesh)
plt.tight_layout()
plt.show()
```

Source: stackoverflow