Last active
September 26, 2023 23:20
-
-
Save thomasweng15/a46f7c92c88f92b9e0424953495ce9cb to your computer and use it in GitHub Desktop.
3D Interactive Visualizations with Plotly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save html to the specified path | |
fig.write_html("plotly_sphere_pts.html") | |
# Save to wandb (call wandb.init() first) | |
# html = plotly.io.to_html(fig) | |
# wandb.log( | |
# { | |
# f"plotly_sphere_pts": wandb.Html(html), | |
# } | |
# ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import trimesh | |
import numpy as np | |
import plotly.graph_objects as go | |
import plotly.figure_factory as ff | |
# Make a mesh sphere | |
mesh = trimesh.primitives.Sphere(radius=1, center=(0, 0, 0)) | |
x, z, y = mesh.vertices.T | |
# Plot mesh | |
fig = ff.create_trisurf( | |
x=x, | |
y=y, | |
z=z, | |
simplices=mesh.faces, | |
aspectratio={"x": 1.0, "y": 1.0, "z": 1.0}, | |
show_colorbar=False, | |
title="", | |
height=500, # in pixels | |
width=500 # in pixels | |
) | |
fig.update_layout( | |
margin=dict( | |
l=5, | |
r=5, | |
b=5, | |
t=5, | |
) | |
) | |
fig.show() # Render result in the browser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add point cloud to figure | |
pts = np.random.uniform(low=-1, high=1, size=(100, 3)) | |
fig.add_trace( | |
go.Scatter3d( | |
x=pts[:, 0], | |
y=pts[:, 2], | |
z=pts[:, 1], | |
mode="markers", | |
marker=dict(size=3, color="blue", opacity=0.8), | |
) | |
) | |
fig.show() # Show the result in the browser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Thomas, thanks a lot for sharing! This is exactly what I am looking for! I optimized it a bit so that it could visualize any trimesh mesh. I would love to share it here in case it might be helpful.