From 00985c119ae164477e8493a69f32f103774cf51c Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Sat, 16 Nov 2019 14:05:19 +0100 Subject: Examples for plotting a simplicial complex with plotly / matplotlib --- src/python/example/plot_alpha_complex.py | 34 ++++++++++++++++++++++++++++++++ src/python/example/plot_rips_complex.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 src/python/example/plot_alpha_complex.py create mode 100755 src/python/example/plot_rips_complex.py (limited to 'src/python/example') diff --git a/src/python/example/plot_alpha_complex.py b/src/python/example/plot_alpha_complex.py new file mode 100755 index 00000000..98931975 --- /dev/null +++ b/src/python/example/plot_alpha_complex.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +import numpy as np +import gudhi +ac = gudhi.AlphaComplex(off_file='../../data/points/tore3D_1307.off') +st = ac.create_simplex_tree() +points = np.array([ac.get_point(i) for i in range(st.num_vertices())]) +# We want to plot the alpha-complex with alpha=0.1. +# We are only going to plot the triangles +triangles = np.array([s[0] for s in st.get_skeleton(2) if len(s[0])==3 and s[1] <= .1]) + +# First possibility: plotly +import plotly.graph_objects as go +fig = go.Figure(data=[ + go.Mesh3d( + x=points[:,0], + y=points[:,1], + z=points[:,2], + i = triangles[:,0], + j = triangles[:,1], + k = triangles[:,2], + ) +]) +fig.show() + +# Second possibility: matplotlib +from mpl_toolkits.mplot3d import Axes3D +import matplotlib.pyplot as plt +fig = plt.figure() +ax = fig.gca(projection='3d') +ax.plot_trisurf(points[:,0], points[:,1], points[:,2], triangles=triangles) +plt.show() + +# Third possibility: mayavi.mlab.triangular_mesh diff --git a/src/python/example/plot_rips_complex.py b/src/python/example/plot_rips_complex.py new file mode 100755 index 00000000..d2637ea8 --- /dev/null +++ b/src/python/example/plot_rips_complex.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +import numpy as np +import gudhi +points = np.array(gudhi.read_off('../../data/points/Kl.off')) +rc = gudhi.RipsComplex(points=points, max_edge_length=.2) +st = rc.create_simplex_tree(max_dimension=2) +# We are only going to plot the triangles +triangles = np.array([s[0] for s in st.get_skeleton(2) if len(s[0])==3]) + +# First possibility: plotly +import plotly.graph_objects as go +fig = go.Figure(data=[ + go.Mesh3d( + # Use the first 3 coordinates, but we could as easily pick others + x=points[:,0], + y=points[:,1], + z=points[:,2], + i = triangles[:,0], + j = triangles[:,1], + k = triangles[:,2], + ) +]) +fig.show() + +# Second possibility: matplotlib +from mpl_toolkits.mplot3d import Axes3D +import matplotlib.pyplot as plt +fig = plt.figure() +ax = fig.gca(projection='3d') +ax.plot_trisurf(points[:,0], points[:,1], points[:,2], triangles=triangles) +plt.show() + +# Third possibility: mayavi.mlab.triangular_mesh -- cgit v1.2.3 From 71ffd4fcbf273289a556ee5ed39a6b8edf8444a1 Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Sat, 16 Nov 2019 14:27:52 +0100 Subject: Add mayavi to the examples --- src/python/example/plot_alpha_complex.py | 5 ++++- src/python/example/plot_rips_complex.py | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'src/python/example') diff --git a/src/python/example/plot_alpha_complex.py b/src/python/example/plot_alpha_complex.py index 98931975..99c18a7c 100755 --- a/src/python/example/plot_alpha_complex.py +++ b/src/python/example/plot_alpha_complex.py @@ -31,4 +31,7 @@ ax = fig.gca(projection='3d') ax.plot_trisurf(points[:,0], points[:,1], points[:,2], triangles=triangles) plt.show() -# Third possibility: mayavi.mlab.triangular_mesh +# Third possibility: mayavi +from mayavi import mlab +mlab.triangular_mesh(points[:,0], points[:,1], points[:,2], triangles); +mlab.show() diff --git a/src/python/example/plot_rips_complex.py b/src/python/example/plot_rips_complex.py index d2637ea8..1c878db1 100755 --- a/src/python/example/plot_rips_complex.py +++ b/src/python/example/plot_rips_complex.py @@ -31,4 +31,8 @@ ax = fig.gca(projection='3d') ax.plot_trisurf(points[:,0], points[:,1], points[:,2], triangles=triangles) plt.show() -# Third possibility: mayavi.mlab.triangular_mesh +# Third possibility: mayavi +# (this may take a while) +from mayavi import mlab +mlab.triangular_mesh(points[:,0], points[:,1], points[:,2], triangles); +mlab.show() -- cgit v1.2.3 From 7880c08d7ff7c42d7b089f9d1252c85289bf7596 Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Sat, 16 Nov 2019 15:11:06 +0100 Subject: Example plot of triangles, edges and vertices with mayavi --- src/python/example/plot_simplex_tree_dim012.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 src/python/example/plot_simplex_tree_dim012.py (limited to 'src/python/example') diff --git a/src/python/example/plot_simplex_tree_dim012.py b/src/python/example/plot_simplex_tree_dim012.py new file mode 100755 index 00000000..e6a1a4e0 --- /dev/null +++ b/src/python/example/plot_simplex_tree_dim012.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +import numpy as np +import gudhi + +# Coordinates of the points +points=np.array([[0,0,0],[1,0,0],[0,1,0],[0,0,1],[1,1,1],[1,1,0],[0,1,1]]) +# Build the simplicial complex with a tetrahedon, an edge and an isolated vertex +cplx=gudhi.SimplexTree() +cplx.insert([1,2,3,5]) +cplx.insert([4,6]) +cplx.insert([0]) + +from mayavi import mlab +# Plot triangles +triangles = np.array([s[0] for s in cplx.get_skeleton(2) if len(s[0])==3]) +mlab.triangular_mesh(points[:,0], points[:,1], points[:,2], triangles); +# 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) +# Plot points +mlab.points3d(points[:,0], points[:,1], points[:,2]) +mlab.show() -- cgit v1.2.3 From 4ed9330dc0da5963124cac1b7e7caebc0eed73cd Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Sat, 16 Nov 2019 17:50:08 +0100 Subject: Draw vertex/edge/triangle with matplotlib --- src/python/example/plot_simplex_tree_dim012.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/python/example') diff --git a/src/python/example/plot_simplex_tree_dim012.py b/src/python/example/plot_simplex_tree_dim012.py index e6a1a4e0..f79c359b 100755 --- a/src/python/example/plot_simplex_tree_dim012.py +++ b/src/python/example/plot_simplex_tree_dim012.py @@ -9,17 +9,37 @@ cplx=gudhi.SimplexTree() cplx.insert([1,2,3,5]) cplx.insert([4,6]) cplx.insert([0]) +triangles = np.array([s[0] for s in cplx.get_skeleton(2) if len(s[0])==3]) +## With matplotlib +from mpl_toolkits.mplot3d import Axes3D +from mpl_toolkits.mplot3d.art3d import Line3DCollection +import matplotlib.pyplot as plt +fig = plt.figure() +ax = fig.gca(projection='3d') +# Plot triangles +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() + +## With mayavi from mayavi import mlab # Plot triangles -triangles = np.array([s[0] for s in cplx.get_skeleton(2) if len(s[0])==3]) 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) -# Plot points -mlab.points3d(points[:,0], points[:,1], points[:,2]) mlab.show() -- cgit v1.2.3 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/python/example') 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