summaryrefslogtreecommitdiff
path: root/src/python/example
diff options
context:
space:
mode:
authorMarc Glisse <marc.glisse@inria.fr>2019-11-22 18:32:45 +0100
committerGitHub <noreply@github.com>2019-11-22 18:32:45 +0100
commit09c3ae9ffb95a6a8a457632ef8a0b85bb93c42a6 (patch)
tree9990d72b7726118c34034b69760902ee621b0765 /src/python/example
parentc8a4a15a88a8db5b8824bcee6d22d552dfdfe625 (diff)
parentfc4f80aa2ce60fd563bb54a0d082c83fe99fd99d (diff)
Merge pull request #143 from mglisse/explot
Add examples of 3d plots of complexes
Diffstat (limited to 'src/python/example')
-rwxr-xr-xsrc/python/example/plot_alpha_complex.py37
-rwxr-xr-xsrc/python/example/plot_rips_complex.py38
-rwxr-xr-xsrc/python/example/plot_simplex_tree_dim012.py66
3 files changed, 141 insertions, 0 deletions
diff --git a/src/python/example/plot_alpha_complex.py b/src/python/example/plot_alpha_complex.py
new file mode 100755
index 00000000..99c18a7c
--- /dev/null
+++ b/src/python/example/plot_alpha_complex.py
@@ -0,0 +1,37 @@
+#!/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
+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
new file mode 100755
index 00000000..1c878db1
--- /dev/null
+++ b/src/python/example/plot_rips_complex.py
@@ -0,0 +1,38 @@
+#!/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
+# (this may take a while)
+from mayavi import mlab
+mlab.triangular_mesh(points[:,0], points[:,1], points[:,2], triangles);
+mlab.show()
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..5b962131
--- /dev/null
+++ b/src/python/example/plot_simplex_tree_dim012.py
@@ -0,0 +1,66 @@
+#!/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])
+# 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
+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
+ax.add_collection3d(Line3DCollection(segments=edges))
+plt.show()
+
+## With mayavi
+from mayavi import mlab
+# Plot triangles
+mlab.triangular_mesh(points[:,0], points[:,1], points[:,2], triangles);
+# Plot points
+mlab.points3d(points[:,0], points[:,1], points[:,2])
+# Plot edges
+for pts in edges:
+ mlab.plot3d(pts[:,0],pts[:,1],pts[:,2],tube_radius=None)
+mlab.show()