Trisurf plots with Plotly in Python
Data visualization is truly one of the highlights of data analysis as involves a graphical representation of data to help extract insights and patterns from raw data. We can enhance our ability to interpret data-driven findings greatly!
Utilizing Plotly's dynamic capabilities, we can now transform raw data into highly interactive plots which are seamless in rendering.
In this answer, we will cover a highly interesting type of data visualization plot, an isosurface plot, using this library.
Plotly
Plotly is a data visualization library that allows us to create rich and dynamic visualizations in Python, R, and JavaScript. We can create plots ranging from line graphs and bar charts to maps and 3D visualizations. Some interesting features of Plotly include the following:
zooming
panning
tooltips
rotating
downloading plots
Let's first understand what isosurface plots are and then implement them.
Trisurf plots
A trisurf plot, a short form of a triangular surface plot, is a type of 3D visualization that represents a surface using triangles.
Simply put, in a trisurf plot, the surface is approximated by a collection of triangles that connect the data points. This provides a continuous representation of the original data.
How do we create trisurf plots?
To create a trisurf plot, we need to have our x, y, and z-axis data points and the connectivity information, i.e., triangles that specify how the points are connected to form the surface. The triangles can be defined using the Delaunay triangulation algorithm, which divides the data points into non-overlapping triangles in the most optimal way.
Syntax
The create_trisurf function in Plotly is used to generate trisurf plots. It mainly requires input arrays for x, y, z coordinates from us and an simplices array specifying the vertex indices for each triangle.
This function simplifies the creation of trisurf plots by handling the triangulation and rendering.
Parameters
Let's take a look at the common parameters for the create_trisurf function.
Parameters | Explanation |
x | y | z | Data values of x, y, and z coordinates |
simplices | Array of vertex indices for each triangle in the triangularization |
colormap | Specifies color scale or color values for the surface |
show_colorbar | Decides if colorbar is visible or not |
scale | Sets scale values for non-linear colormap interpolation |
color_func | Controls coloring of the surface using functions |
title | The plot title |
plot_edges | Determines visibility of triangles' edges on the trisurface |
showbackground | Makes background in plot visible |
backgroundcolor | Background color of the plot |
gridcolor | Color of gridlines beside the axes |
zerolinecolor | Color of the axes |
edges_color | Color of the edges if |
height | The plot height in pixels |
width | The plot width in pixels |
aspectratio | Dictionary of aspect ratio for all 3 axes |
Code implementation
Let's create a trisurf plot ourselves now! You can experiment with it yourself and click "Run" once you're done.
import plotly.figure_factory as ff
import numpy as np
from scipy.spatial import Delaunay
u = np.linspace(0, 2*np.pi, 40)
v = np.linspace(0, 2*np.pi, 40)
u, v = np.meshgrid(u, v)
u = u.flatten()
v = v.flatten()
x = (3 + 1.5 * np.cos(v)) * np.cos(u)
y = (3 + 1.5 * np.cos(v)) * np.sin(u)
z = 1.5 * np.sin(v)
points2D = np.vstack([u, v]).T
tri = Delaunay(points2D)
simplices = tri.simplices
fig = ff.create_trisurf(x=x, y=y, z=z,
simplices=simplices,
title= "Torus / Donut Visualization", aspectratio=dict(x=1, y=1, z=0.6))
fig.show()
fig.write_html("output.html", auto_open=True)Code explanation
Lines 1–3: We begin by importing our needed modules,
plotly.figure_factoryasfffor creating the visualization, andnumpyasnpandDelaunayfromscipy.spatialfor our calculations.Lines 5–6: Next, we generate parameter values
uandvfor the shape, usingnp.linspace().Lines 7–9: We also create a 2D grid using
np.meshgrid()by passinguandv, for further calculations. The flattened values of these two variables are saved.Lines 11–13: Now, we calculate the coordinates
x,y, andzfor the torus using some parametric equations. These equations can be changed according to the shape needed.Lines 15–17: We stack
uandvpoints to createpoints2Dso that we can perform theDelaunaytriangulation to definesimplicesfor the torus' surface triangles.Line 19: Yay, we can now create a triangular surface plot using
ff.create_trisurf(), providing the coordinatesx,y, andz, andsimplices. We can also customize the plot title and aspect ratio.Line 22: We use
fig.show()to display our 3D plot on the browser.
Note: To save the output in an HTML file,
fig.write_html("output.html", auto_open=True)can be used.
Demonstration
This is what a trisurf plot in the shape of a torus i.e. doughnut-like structure looks like.
Alternate example
We can change the shape of our trisurf plots by changing the equations within the code. Let's create a wave-like structure.
import plotly.figure_factory as ff
import numpy as np
from scipy.spatial import Delaunay
u = np.linspace(-5, 5, 200)
v = np.linspace(-5, 5, 100)
u, v = np.meshgrid(u, v)
u = u.flatten()
v = v.flatten()
x = u
y = v
z = np.sin(np.sqrt(u**2 + v**2)) * np.cos(u) + np.cos(np.sqrt(u**2 + v**2)) * np.sin(v)
points2D = np.vstack([u, v]).T
tri = Delaunay(points2D)
simplices = tri.simplices
fig = ff.create_trisurf(x=x, y=y, z=z,
simplices=simplices,
title="Rolling Wave Surface",
aspectratio=dict(x=1, y=1, z=0.6),
)
fig.show()
fig.write_html("output.html", auto_open=True)Demonstration
This is how the trisurf in the form of a wave-like structure looks like.
Use cases of trisurf plots
Trisurf plots can be used to aid applications within the 3D realm significantly. Let's take a look at a few use cases of trisurf plots below.
Terrain visualization
Trisurf plots can be used to show terrains and landscapes effectively.
Structural analysis
Trisurf plots are great at visualizing stress and deformation patterns in structures for engineering design.
Material properties
Similarly, trisurf plots can analyze material behaviors and crystal structures in material science research.
Medical simulation
These plots can visualize medical procedures for training and research.
Astronomical visualization
One fun use case is in studying celestial bodies and galaxy distributions in astrophysics.
What does the statement tri = Delaunay(points2D) mean?
Free Resources