diff options
author | Marc Glisse <marc.glisse@inria.fr> | 2019-11-16 14:05:19 +0100 |
---|---|---|
committer | Marc Glisse <marc.glisse@inria.fr> | 2019-11-16 14:05:19 +0100 |
commit | 00985c119ae164477e8493a69f32f103774cf51c (patch) | |
tree | dcb2c6ff4b1f6280f52a0b70c6c169780ae5a66a /src/python | |
parent | 184587947b18daf57583c7cb111ad315b8d48a43 (diff) |
Examples for plotting a simplicial complex with plotly / matplotlib
Diffstat (limited to 'src/python')
-rwxr-xr-x | src/python/example/plot_alpha_complex.py | 34 | ||||
-rwxr-xr-x | src/python/example/plot_rips_complex.py | 34 |
2 files changed, 68 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..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 |