Interactive 3D Plots with Plotly
I’ve recently had to plot a lot of 3D elements like point clouds and meshes for my research. Compared to 2D plots, 3D plots benefit greatly from interaction: translating and rotating the plot gives a better sense of the geometry of elements in the plot. I’ve found that plotly is the easiest and most responsive tool for interactive 3D plots, and this post is a primer on how I make them.
Visualizing meshes with plotly
Below is an example of a sphere rendered with Python and plotly.
The code is straightforward: pass the vertices and faces of a mesh to plotly’s create_trisurf()
function.
Adding other elements to the same plot
Use the add_trace()
function to add other elemnts, e.g. a point cloud, to the same figure.
Saving the plot
Besides using fig.show()
to show the plot, you can save it as html, or even log it to wandb:
Why not matplotlib or trimesh?
3D plotting in matplotlib is extremely slow. The trimesh Scene visualizer is also slower than plotly. Interactive features for both matplotlib and trimesh become unusable as the number of elements in the plot grows.
Plotly, on the other hand, uses a webGL backend to render in the browser. If you have hardware acceleration turned on for your browser it is even faster. I haven’t tried visualizers from other libraries like open3d, as I was pretty happy with plotly once I started using it.
Here is the link to the Github Gist with all the code in this post. Hope it helps!