Show Sidebar Hide Sidebar

# Trisurf Plots in Python

How to make tri-surf plots in Python with Plotly. Trisurfs are formed by replacing the boundaries of a compact surface by touching triangles.

#### New to Plotly?¶

You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

#### Version Check¶

Note: Trisurfs are available in version 1.11.0+
Run pip install plotly --upgrade to update your Plotly version

In [1]:
import plotly
plotly.__version__

Out[1]:
'2.0.0'

#### Torus¶

In [2]:
import plotly.plotly as py
import plotly.figure_factory as FF
from plotly.graph_objs import graph_objs

import numpy as np
from scipy.spatial import Delaunay

u = np.linspace(0, 2*np.pi, 20)
v = np.linspace(0, 2*np.pi, 20)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

x = (3 + (np.cos(v)))*np.cos(u)
y = (3 + (np.cos(v)))*np.sin(u)
z = np.sin(v)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

fig1 = FF.create_trisurf(x=x, y=y, z=z,
simplices=simplices,
title="Torus", aspectratio=dict(x=1, y=1, z=0.3))
py.iplot(fig1, filename="3dFolder/Torus")

Out[2]:

#### Mobius Band¶

In [3]:
import plotly.plotly as py
import plotly.figure_factory as FF
from plotly.graph_objs import graph_objs

import numpy as np
from scipy.spatial import Delaunay

u = np.linspace(0, 2*np.pi, 24)
v = np.linspace(-1, 1, 8)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

tp = 1 + 0.5*v*np.cos(u/2.)
x = tp*np.cos(u)
y = tp*np.sin(u)
z = 0.5*v*np.sin(u/2.)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

fig1 = FF.create_trisurf(x=x, y=y, z=z,
colormap="Portland",
simplices=simplices,
title="Mobius Band")
py.iplot(fig1, filename="Mobius-Band")

Out[3]:

#### Boy's Surface¶

In [4]:
import plotly.plotly as py
import plotly.figure_factory as FF
from plotly.graph_objs import graph_objs

import numpy as np
from scipy.spatial import Delaunay

u=np.linspace(-np.pi/2, np.pi/2, 60)
v=np.linspace(0, np.pi, 60)
u,v=np.meshgrid(u,v)
u=u.flatten()
v=v.flatten()

x = (np.sqrt(2)*(np.cos(v)*np.cos(v))*np.cos(2*u) + np.cos(u)*np.sin(2*v))/(2 - np.sqrt(2)*np.sin(3*u)*np.sin(2*v))
y = (np.sqrt(2)*(np.cos(v)*np.cos(v))*np.sin(2*u) - np.sin(u)*np.sin(2*v))/(2 - np.sqrt(2)*np.sin(3*u)*np.sin(2*v))
z = (3*(np.cos(v)*np.cos(v)))/(2 - np.sqrt(2)*np.sin(3*u)*np.sin(2*v))

points2D = np.vstack([u, v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

fig1 = FF.create_trisurf(x=x, y=y, z=z,
colormap=['rgb(50, 0, 75)', 'rgb(200, 0, 200)', '#c8dcc8'],
show_colorbar=True,
simplices=simplices,
title="Boy's Surface")
py.iplot(fig1, filename="Boy's Surface")

Out[4]:

#### Change Colorscale Variable¶

In [5]:
import plotly.plotly as py
import plotly.figure_factory as FF
from plotly.graph_objs import graph_objs

import numpy as np
from scipy.spatial import Delaunay

u = np.linspace(0, 2*np.pi, 20)
v = np.linspace(0, 2*np.pi, 20)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

x = (3 + (np.cos(v)))*np.cos(u)
y = (3 + (np.cos(v)))*np.sin(u)
z = np.sin(v)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

# define a function that calculates the distance
# from the origin to use as the color variable
def dist_origin(x, y, z):
return np.sqrt((1.0 * x)**2 + (1.0 * y)**2 + (1.0 * z)**2)

fig1 = FF.create_trisurf(x=x, y=y, z=z, color_func=dist_origin,
colormap = [(0.4, 0.15, 0), (1, 0.65, 0.12)],
show_colorbar=True,
simplices=simplices, title="Torus - Origin Distance Coloring",
aspectratio=dict(x=1, y=1, z=0.3))
py.iplot(fig1, filename="Torus - Origin Distance Coloring")

Out[5]:

#### Diverging Colormap¶

In [6]:
import plotly.plotly as py
import plotly.figure_factory as FF
from plotly.graph_objs import graph_objs

import numpy as np
from scipy.spatial import Delaunay

u = np.linspace(-np.pi, np.pi, 30)
v = np.linspace(-np.pi, np.pi, 30)
u, v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

x = u
y = u*np.cos(v)
z = u*np.sin(v)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

# define a function for the color assignment
def dist_from_x_axis(x, y, z):
return x

fig1 = FF.create_trisurf(x=x, y=y, z=z,
colormap=['rgb(255, 155, 120)', 'rgb(255, 153, 255)', ],
show_colorbar=True,
simplices=simplices, title="Light Cone",
showbackground=False, gridcolor='rgb(255, 20, 160)',
plot_edges=False, aspectratio=dict(x=1, y=1, z=0.75))
py.iplot(fig1, filename="Light Cone")

Out[6]:

#### Reference¶

In [7]:
help(FF.create_trisurf)

Help on function create_trisurf in module plotly.figure_factory._trisurf:

create_trisurf(x, y, z, simplices, colormap=None, show_colorbar=True, scale=None, color_func=None, title='Trisurf Plot', plot_edges=True, showbackground=True, backgroundcolor='rgb(230, 230, 230)', gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', edges_color='rgb(50, 50, 50)', height=800, width=800, aspectratio=None)
Returns figure for a triangulated surface plot

:param (array) x: data values of x in a 1D array
:param (array) y: data values of y in a 1D array
:param (array) z: data values of z in a 1D array
:param (array) simplices: an array of shape (ntri, 3) where ntri is
the number of triangles in the triangularization. Each row of the
array contains the indicies of the verticies of each triangle
:param (str|tuple|list) colormap: either a plotly scale name, an rgb
or hex color, a color tuple or a list of colors. An rgb color is
of the form 'rgb(x, y, z)' where x, y, z belong to the interval
[0, 255] and a color tuple is a tuple of the form (a, b, c) where
a, b and c belong to [0, 1]. If colormap is a list, it must
contain the valid color types aforementioned as its members
:param (bool) show_colorbar: determines if colorbar is visible
:param (list|array) scale: sets the scale values to be used if a non-
linearly interpolated colormap is desired. If left as None, a
linear interpolation between the colors will be excecuted
:param (function|list) color_func: The parameter that determines the
coloring of the surface. Takes either a function with 3 arguments
x, y, z or a list/array of color values the same length as
simplices. If None, coloring will only depend on the z axis
:param (str) title: title of the plot
:param (bool) plot_edges: determines if the triangles on the trisurf
are visible
:param (bool) showbackground: makes background in plot visible
:param (str) backgroundcolor: color of background. Takes a string of
the form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive
:param (str) gridcolor: color of the gridlines besides the axes. Takes
a string of the form 'rgb(x,y,z)' x,y,z are between 0 and 255
inclusive
:param (str) zerolinecolor: color of the axes. Takes a string of the
form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive
:param (str) edges_color: color of the edges, if plot_edges is True
:param (int|float) height: the height of the plot (in pixels)
:param (int|float) width: the width of the plot (in pixels)
:param (dict) aspectratio: a dictionary of the aspect ratio values for
the x, y and z axes. 'x', 'y' and 'z' take (int|float) values

Example 1: Sphere

# Necessary Imports for Trisurf
import numpy as np
from scipy.spatial import Delaunay

import plotly.plotly as py
from plotly.figure_factory import create_trisurf
from plotly.graph_objs import graph_objs

# Make data for plot
u = np.linspace(0, 2*np.pi, 20)
v = np.linspace(0, np.pi, 20)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

x = np.sin(v)*np.cos(u)
y = np.sin(v)*np.sin(u)
z = np.cos(v)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

# Create a figure
fig1 = create_trisurf(x=x, y=y, z=z, colormap="Rainbow",
simplices=simplices)
# Plot the data
py.iplot(fig1, filename='trisurf-plot-sphere')


Example 2: Torus

# Necessary Imports for Trisurf
import numpy as np
from scipy.spatial import Delaunay

import plotly.plotly as py
from plotly.figure_factory import create_trisurf
from plotly.graph_objs import graph_objs

# Make data for plot
u = np.linspace(0, 2*np.pi, 20)
v = np.linspace(0, 2*np.pi, 20)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

x = (3 + (np.cos(v)))*np.cos(u)
y = (3 + (np.cos(v)))*np.sin(u)
z = np.sin(v)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

# Create a figure
fig1 = create_trisurf(x=x, y=y, z=z, colormap="Viridis",
simplices=simplices)
# Plot the data
py.iplot(fig1, filename='trisurf-plot-torus')


Example 3: Mobius Band

# Necessary Imports for Trisurf
import numpy as np
from scipy.spatial import Delaunay

import plotly.plotly as py
from plotly.figure_factory import create_trisurf
from plotly.graph_objs import graph_objs

# Make data for plot
u = np.linspace(0, 2*np.pi, 24)
v = np.linspace(-1, 1, 8)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

tp = 1 + 0.5*v*np.cos(u/2.)
x = tp*np.cos(u)
y = tp*np.sin(u)
z = 0.5*v*np.sin(u/2.)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

# Create a figure
fig1 = create_trisurf(x=x, y=y, z=z, colormap=[(0.2, 0.4, 0.6), (1, 1, 1)],
simplices=simplices)
# Plot the data
py.iplot(fig1, filename='trisurf-plot-mobius-band')


Example 4: Using a Custom Colormap Function with Light Cone

# Necessary Imports for Trisurf
import numpy as np
from scipy.spatial import Delaunay

import plotly.plotly as py
from plotly.figure_factory import create_trisurf
from plotly.graph_objs import graph_objs

# Make data for plot
u=np.linspace(-np.pi, np.pi, 30)
v=np.linspace(-np.pi, np.pi, 30)
u,v=np.meshgrid(u,v)
u=u.flatten()
v=v.flatten()

x = u
y = u*np.cos(v)
z = u*np.sin(v)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

# Define distance function
def dist_origin(x, y, z):
return np.sqrt((1.0 * x)**2 + (1.0 * y)**2 + (1.0 * z)**2)

# Create a figure
fig1 = create_trisurf(x=x, y=y, z=z,
colormap=['#FFFFFF', '#E4FFFE',
'#A4F6F9', '#FF99FE',
'#BA52ED'],
scale=[0, 0.6, 0.71, 0.89, 1],
simplices=simplices,
color_func=dist_origin)
# Plot the data
py.iplot(fig1, filename='trisurf-plot-custom-coloring')


Example 5: Enter color_func as a list of colors

# Necessary Imports for Trisurf
import numpy as np
from scipy.spatial import Delaunay
import random

import plotly.plotly as py
from plotly.figure_factory import create_trisurf
from plotly.graph_objs import graph_objs

# Make data for plot
u=np.linspace(-np.pi, np.pi, 30)
v=np.linspace(-np.pi, np.pi, 30)
u,v=np.meshgrid(u,v)
u=u.flatten()
v=v.flatten()

x = u
y = u*np.cos(v)
z = u*np.sin(v)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

colors = []
color_choices = ['rgb(0, 0, 0)', '#6c4774', '#d6c7dd']

for index in range(len(simplices)):
colors.append(random.choice(color_choices))

fig = create_trisurf(
x, y, z, simplices,
color_func=colors,
show_colorbar=True,
edges_color='rgb(2, 85, 180)',
title=' Modern Art'
)

py.iplot(fig, filename="trisurf-plot-modern-art")



Still need help?