Issue with highlighting edges of the 3D polyhedra

Hi @Alesya,

Mesh3d does not return the triangulation defined from your data points, but you can proceed as follows to get your desired plot:

  • define the triangulation using Delaunay function from scipy.spatial.
  • Delanay returns the array of simplices from which, with a few lines of code, you can get the triangles and plot their edges, while the mesh is plotted using another version of go.Mesh3d.

Example:

from scipy.spatial import Delaunay
import numpy as np
import plotly.graph_objects as go

mydata= -3+5*np.random.rand(18)
pts = mydata.reshape((6,3))
mesh = Delaunay(pts)

# mesh.simplices returns indices of points that form tetrahedra
# a simplex is in this case a 4-tuple of ints


#extract tetrahedra faces:
tri =[]
for s in mesh.simplices:
    tri.extend([(s[i], s[i+1], s[(i+2)%4])  for  i in range(3)])

utri = list(set(tri))  #utri is the list of unique triangles
I, J, K = np.asarray(utri).T
x, y, z = mesh.points.T
verts = mesh.points

tri_vertices= verts[np.asarray(utri)]
Xe = []
Ye = []
Ze = []
for T in tri_vertices:
    Xe += [T[k%3][0] for k in range(4)]+[ None]
    Ye += [T[k%3][1] for k in range(4)]+[ None]
    Ze += [T[k%3][2] for k in range(4)]+[ None]
       
#define the trace consisting in all triangle edges
lines= go.Scatter3d(
            x=Xe,
            y=Ye,
            z=Ze,
            mode='lines',
            name='',
            line=dict(color= 'rgb(50,50,50)', width=1.5)) 
fig=go.Figure(go.Scatter3d(x=x, y=y, z=z, mode="markers", marker_size=3))
fig.add_mesh3d(x=x, y=y, z=z, i=I, j=J, k=K, color='rgba(0, 218, 245, 0.35)')
fig.add_trace(lines)
fig.update_layout(width=600, height=600, showlegend=False)

1 Like