GUDHI / TDA-tutorial

A set of jupyter notebooks for the practice of TDA with the python Gudhi library together with popular machine learning and data sciences libraries.
MIT License
373 stars 112 forks source link

Visualization of simplicial complexes #6

Closed mglisse closed 4 years ago

mglisse commented 4 years ago

People regularly ask how to visualize a simplicial complex, I think we should update the tutorial to show some ways to plot. As examples with 2 different libraries, first alpha complex

import numpy as np
import gudhi
ac = gudhi.AlphaComplex(off_file='/home/glisse/repos/gudhi/data/points/tore3D_1307.off')
st = ac.create_simplex_tree()
triangles = np.array([s[0] for s in st.get_skeleton(2) if len(s[0])==3 and s[1] <= .1])
points = np.array([ac.get_point(i) for i in range(st.num_vertices())])

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()

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()

and Rips complex (we could pick different coordinates than the first 3 for the projection, possibly multiply by a 5x3 matrix)

import numpy as np
import gudhi
points = np.array(gudhi.read_off('/home/glisse/repos/gudhi/data/points/Kl.off'))
rc = gudhi.RipsComplex(points=points,max_edge_length=.2)
st = rc.create_simplex_tree(max_dimension=2)
triangles = np.array([s[0] for s in st.get_skeleton(2) if len(s[0])==3])

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()

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()
mglisse commented 4 years ago

I am going to add examples to the library with https://github.com/GUDHI/gudhi-devel/pull/143, but it would still be useful to have something in one of the tutorials.