Python

Plotly.Graph_objects.mesh3d

“Mesh plots are types of plots that use a set of triangles with the given points of x, y, and z coordinates.
In this article, we will explore how to create 3-dimensional mesh diagrams using the graph_objects module.”

Let’s dive in.

Simple Mesh Diagram

We can create a simple 3d mesh plot by specifying the x, y, and z values to the mesh3d class.

Take the code snippet provided below:

import plotly.graph_objects as go
x = [0,1,2,0]
y = [0,0,1,2]
z = [0,2,0,1]
fig = go.Figure(data=go.Mesh3d(
    x=x, y=y, z=z
))
fig.show()

The code above will create a simple tetrahedron, as shown in the output below:

To create a complete tetrahedron plot, you can specify the i, j, and k parameters.

Mesh Diagram From Dataset

Similarly, you can create a mesh diagram from a dataset by specifying the x, y, and x parameters as columns within the DataFrame.

An example is as shown:

import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/ply/sphere-ply.csv')
fig = go.Figure(data=go.Mesh3d(
    x=df['x'],
    y=df['y'],
    z=df['z'],
    i=df['i'],
    j=df['j'],
    k=df['k']
))
fig.show()

The code above will load the sphere dataset into the df variable. We then plot the sphere by specifying the x, y, z, i, j, and k parameters as the columns in the DataFram.

The resulting figure is as shown:

We can also set a different color using the facecolor parameter as shown:

fig = go.Figure(data=go.Mesh3d(
    x=df['x'],
    y=df['y'],
    z=df['z'],
    i=df['i'],
    j=df['j'],
    k=df['k'],
    facecolor=df['facecolor']
))

Termination

In this tutorial, we explored how to create 3d mesh diagrams using Mesh3d class from the Plotly graph_objects module.

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