0

I wonder if mplot3d provides a way to alter the opacity of mesh face colors. Below is a simple example for creating a 3Dplot using mplot3D and marching_squares method.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection 
from mpl_toolkits.mplot3d import Axes3D

# create 3D numpy array, called mask
mask = np.zeros((3,3,3))
for i in np.arange(0,1):
    for j in np.arange(0,1):
        for k in np.arange(0,1):
            mask[i,j,k] = 1

# use module (in this case, marching cubes) to find the vertices and faces of this 3D object

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')
# set face color
mesh.set_facecolor('r')
ax.add_collection3d(mesh)
plt.tight_layout()
plt.show()

Is there a function to set the face color as transparent (i.e. can you alter face opacity?) I searched around and have not found the right function, as yet.

McM
  • 471
  • 5
  • 21
  • Did you try [mesh.set_alpha(...)](https://matplotlib.org/api/_as_gen/mpl_toolkits.mplot3d.art3d.Poly3DCollection.html#mpl_toolkits.mplot3d.art3d.Poly3DCollection.set_alpha)? – JohanC Jul 13 '20 at 22:48
  • Interesting. I have not, however I may have found a solution here: https://stackoverflow.com/questions/23403293/3d-surface-not-transparent-inspite-of-setting-alpha – McM Jul 13 '20 at 22:50
  • I think that answer is old and that now the default `Poly3DCollection(verts[faces], alpha=...)` works OK. You can also directly set the face and edgecolors (`Poly3DCollection(verts[faces], alpha=..., fc='r', ec='k')`). – JohanC Jul 13 '20 at 22:59
  • Oh! That's so simple. I love python. So I would just do a ```mesh = Poly3DCollection(verts[faces],alpha=0.5,fc='r',ec='k')```. Thanks ! – McM Jul 14 '20 at 01:43

0 Answers0