Show Sidebar Hide Sidebar

3D Mesh Plots in Python

How to make 3D Mesh Plots

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
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: 3D Mesh are available in version 1.10.0+
Run pip install plotly --upgrade to update your Plotly version

In [1]:
import plotly
plotly.__version__
Out[1]:
'1.11.0'

In the following example, we will make a simple 3D Mesh plot.

Simple 3D Mesh example

We are using data present in a separate text file. The file can be downloaded from here

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

pts=np.loadtxt('dataset.txt')
x,y,z=zip(*pts)

trace = go.Mesh3d(x=x,y=y,z=z,color='90EE90',opacity=0.50)
py.iplot([trace])
Out[2]:

3D Mesh example with Alphahull

Alphahull sets shape of mesh. If the value is -1 then Delaunay triangulation is used. If >0 then the alpha-shape algorithm is used. The default value is -1.

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

pts=np.loadtxt('dataset.txt')
x,y,z=zip(*pts)

trace = go.Mesh3d(x=x,y=y,z=z,
                   alphahull=5,
                   opacity=0.4,
                   color='00FFFF')
py.iplot([trace])
Out[3]:

Mesh Tetrahedron

In [6]:
data = go.Data([
    go.Mesh3d(
        x = [0, 1, 2, 0],
        y = [0, 0, 1, 2],
        z = [0, 2, 0, 1],
        colorbar = go.ColorBar(
            title='z'
        ),
        colorscale = [['0', 'rgb(255, 0, 0)'], ['0.5', 'rgb(0, 255, 0)'], ['1', 'rgb(0, 0, 255)']],
        intensity = [0, 0.33, 0.66, 1],
        i = [0, 0, 0, 1],
        j = [1, 2, 3, 2],
        k = [2, 3, 1, 3],
        name = 'y',
        showscale = True
    )
])
layout = go.Layout(
    xaxis=go.XAxis(
        title='x'
    ),
    yaxis=go.YAxis(
        title='y'
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='3d-mesh-tetrahedron-python')
Out[6]:

Mesh Cube

In [3]:
data = go.Data([
    go.Mesh3d(
        x = [0, 0, 1, 1, 0, 0, 1, 1],
        y = [0, 1, 1, 0, 0, 1, 1, 0],
        z = [0, 0, 0, 0, 1, 1, 1, 1],
        colorbar = go.ColorBar(
            title='z'
        ),
        colorscale = [['0', 'rgb(255, 0, 255)'], ['0.5', 'rgb(0, 255, 0)'], ['1', 'rgb(0, 0, 255)']],
        intensity = [0, 0.142857142857143, 0.285714285714286, 0.428571428571429, 0.571428571428571, 0.714285714285714, 0.857142857142857, 1],
        i = [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2],
        j = [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3],
        k = [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6],
        name='y',
        showscale=True
    )
])
layout = go.Layout(
    xaxis=go.XAxis(
        title='x'
    ),
    yaxis=go.YAxis(
        title='y'
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='3d-mesh-cube-python')
Out[3]:

Reference

See https://plot.ly/python/reference/#mesh3d for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.