From fc4f80aa2ce60fd563bb54a0d082c83fe99fd99d Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Sat, 16 Nov 2019 18:35:44 +0100 Subject: Draw vertex/edge/triangle with plotly --- src/python/example/plot_simplex_tree_dim012.py | 41 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/python/example/plot_simplex_tree_dim012.py b/src/python/example/plot_simplex_tree_dim012.py index f79c359b..5b962131 100755 --- a/src/python/example/plot_simplex_tree_dim012.py +++ b/src/python/example/plot_simplex_tree_dim012.py @@ -9,7 +9,36 @@ cplx=gudhi.SimplexTree() cplx.insert([1,2,3,5]) cplx.insert([4,6]) cplx.insert([0]) +# List of triangles (point indices) triangles = np.array([s[0] for s in cplx.get_skeleton(2) if len(s[0])==3]) +# List of edges (point coordinates) +edges = [] +for s in cplx.get_skeleton(1): + e = s[0] + if len(e) == 2: + edges.append(points[[e[0],e[1]]]) + +## With plotly +import plotly.graph_objects as go +# Plot triangles +f2 = go.Mesh3d( + x=points[:,0], + y=points[:,1], + z=points[:,2], + i = triangles[:,0], + j = triangles[:,1], + k = triangles[:,2], + ) +# Plot points +f0 = go.Scatter3d(x=points[:,0], y=points[:,1], z=points[:,2], mode="markers") +data = [f2, f0] +# Plot edges +for pts in edges: + seg = go.Scatter3d(x=pts[:,0],y=pts[:,1],z=pts[:,2],mode="lines",line=dict(color='green')) + data.append(seg) +fig = go.Figure(data=data,layout=dict(showlegend=False)) +# By default plotly would give each edge its own color and legend, that's too much +fig.show() ## With matplotlib from mpl_toolkits.mplot3d import Axes3D @@ -22,11 +51,6 @@ ax.plot_trisurf(points[:,0], points[:,1], points[:,2], triangles=triangles) # Plot points ax.scatter3D(points[:,0], points[:,1], points[:,2]) # Plot edges -edges=[] -for s in cplx.get_skeleton(1): - e = s[0] - if len(e) == 2: - edges.append(points[[e[0],e[1]]]) ax.add_collection3d(Line3DCollection(segments=edges)) plt.show() @@ -37,9 +61,6 @@ mlab.triangular_mesh(points[:,0], points[:,1], points[:,2], triangles); # Plot points mlab.points3d(points[:,0], points[:,1], points[:,2]) # Plot edges -for s in cplx.get_skeleton(1): - e = s[0] - if len(e) == 2: - pts = points[[e[0],e[1]]] - mlab.plot3d(pts[:,0],pts[:,1],pts[:,2],tube_radius=None) +for pts in edges: + mlab.plot3d(pts[:,0],pts[:,1],pts[:,2],tube_radius=None) mlab.show() -- cgit v1.2.3