3D Isosurface Plots in Python/v3

How to make 3D Isosurface Plots in Python with Plotly.

Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.
The version 4 version of this page is here.

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!

Basic Isosurface¶

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [go.Isosurface(
x=[0,0,0,0,1,1,1,1],
y=[1,0,1,0,1,0,1,0],
z=[1,1,0,0,1,1,0,0],
value=[1,2,3,4,5,6,7,8],
isomin=2,
isomax=6
)]

py.iplot(data, filename='basic-isosurface-trace')

Out[1]:

Isosurface with Addtional Slices¶

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

import numpy as np

f = lambda x, y, z:  81*(x**3 + y**3 + z**3) - 189*(x**2*y + x**2*z + y**2*x +y**2*z + z**2*x + z**2*y) +\
54*(x*y*z) + 126*(x*y + x*z + y*z) - 9*(x**2 + y**2 + z**2) - 9*(x + y + z) + 1

a = 1
X, Y, Z = np.mgrid[-a:a:25j, -a:a:25j, -a:a:25j]

data = [go.Isosurface(
x=X.flatten(),
y=Y.flatten(),
z=Z.flatten(),
value=f(X, Y, Z).flatten(),
isomin=-100,
isomax=100,
surface=dict(show=True,count=1, fill=0.8),
slices=go.isosurface.Slices(
z=go.isosurface.slices.Z(
show = True,
locations=[-0.3, 0.5])
),
caps=go.isosurface.Caps(
z=dict(show=False),
x=dict(show=False),
y=dict(show=False)
),
)]

layout = go.Layout(
margin=dict(t=0, l=0, b=0),
scene=dict(
camera=dict(
eye=dict(
x=1.86,
y=0.61,
z=0.98
)
)
)
)

fig = go.Figure(data, layout)

py.iplot(fig, config=dict(showSendToCloud=True), filename='isosurface-with-slices')

Out[2]:

Multiple Isosurfaces with Caps¶

In [6]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.io as pio

import numpy as np

f = lambda x, y, z:  81*(x**3 + y**3 + z**3) - 189*(x**2*y + x**2*z + y**2*x +y**2*z + z**2*x + z**2*y) +\
54*(x*y*z) + 126*(x*y + x*z + y*z) - 9*(x**2 + y**2 + z**2) - 9*(x + y + z) + 1

a = 1
X, Y, Z = np.mgrid[-a:a:25j, -a:a:25j, -a:a:25j]

data = [go.Isosurface(
x=X.flatten(),
y=Y.flatten(),
z=Z.flatten(),
value=f(X, Y, Z).flatten(),
isomin=-10,
isomax=10,
surface=dict(show=True,count=4, fill=0.8, pattern='odd'),
caps=go.isosurface.Caps(
z=dict(show=True),
x=dict(show=True),
y=dict(show=True)
),
)]

layout = go.Layout(
margin=dict(t=0, l=0, b=0),
template=pio.templates['plotly'],
scene=dict(
camera=dict(
eye=dict(
x=1.86,
y=0.61,
z=0.98
)
)
)
)

fig = go.Figure(data, layout)

py.iplot(fig, config=dict(showSendToCloud=True), filename='multiple-isosurface-with-caps')

Out[6]:

Reference¶

See https://plotly.com/python/reference/#isosurface for more information and chart attribute options!