Python

Plotly.Figure_Create.Trisurf

In this article, we will discuss how we can create a trisurf plot using the Plotly figure_factory module.

Function Syntax and Parameters

The function syntax is as shown in the following:

plotly.figure_factory.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)

The required parameters are as follows:

  1. x – Defines the data values of x.
  2. y – Sets the data values of y.
  3. z – Sets the data values of y.
  4. show_colorbar – Sets if the colorbar is visible or not.
  5. Title – Defines the title for the plot.
  6. width/height – Sets the figure width and height in pixels.

Delaunay Plot with Plotly Figure_Factory

The following code uses Plotly figure_factory, NumPy, and SciPy to create a Delaunay plot.

from plotly.figure_factory import create_trisurf
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
fig = create_trisurf(x=x, y=y, z=z)
fig.show()

Output Figure:

Mobius Strip Trisurf

The following code creates a Mobius strip with Plotly figure_factory.

import plotly.figure_factory as ff
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
fig = ff.create_trisurf(x=x, y=y, z=z,
                         colormap="Viridis",
                         simplices=simplices,
                         title="Mobius Strip")
fig.show()

Output Figure:

Conclusion

This guide illustrates how to create the trisurf plots with Plotly figure_factory module. Check the docs for more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list