Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

Issue with highlighting edges of the 3D polyhedra

Hello everyone!

I only started to use Plotly library and I need your help! Probably, my question is very easy.
I created a 3D surface defined by convexhull coordinates of a set of points. As a result, I have all points using go.Scatter3d (some of them inside, some of them as corners of created polyhedra), and a 3D surface using go.Mesh3d, but I want to highlight each edge between points of polyhedra to make the surface more visible.
Now I have this code:

fig = go.Figure()

polyhedra_points = [ ]
coordinates = result_fin[[‘Teta_N’, ‘Teta_V’, ‘Teta_chi’]].values
hull = scipy.spatial.ConvexHull(coordinates)

polyhedra_points = hull.vertices.tolist()
coordinates_all = result_fin[[‘Teta_N’, ‘Teta_V’, ‘Teta_chi’]].to_numpy()
polyhedra_coordinates = coordinates_all[polyhedra_points]

fig = go.Figure(data=[go.Scatter3d(x=result_fin.Teta_N, y=result_fin.Teta_V, z=result_fin.Teta_chi,
mode=‘markers’,
marker=dict(size = 2, color = ‘blue’),
)
]
)

fig.add_trace(go.Mesh3d(x=polyhedra_coordinates[:, 0],
y=polyhedra_coordinates[:, 1],
z=polyhedra_coordinates[:, 2],
text = polyhedra_df.composition, #we see only the names of the polyhedra points
color=“blue”,
name=‘Laves’,
contour = dict(color = ‘blue’, show = True, width = 10),
#facecolor = polyhedra_coordinates,
flatshading=True, #helped a bit to see edges
lighting=dict(ambient=0.6, diffuse=0.8), #helped a bit to see edges
opacity=.4,
alphahull=0
),
)

fig.update_traces(showlegend=True)
fig.update_layout(title=“Structural 3D map”,
legend_title=‘Phase name’,
scene=dict(
xaxis = dict(title=r’Theta_N’, nticks=10, range=[-1,1]),
yaxis = dict(title=r’Theta_V’, nticks=10, range=[-1,1]),
zaxis = dict(title=r’Theta_chi’, nticks=10, range=[-1,1]))
)

fig.show()

With this code I have this plot:

I want to have something like this (this plot was done in matplotlib, I need edges like in it):

I marked as a comment #facecolor = polyhedra_coordinates - maybe using this parameter is it possible to highlight edges with the same colour that the points, but by this way, as I put it, it didn’t work. So, if I use this facecolor parameter then I have this result, but not highlighted edges:

Can you give me some recommendations how to get highlighted edges?
Thanks in advance!

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)

Thank you! It works now!