Show Sidebar Hide Sidebar

3D Scatter Plots in Python

How to make 3D scatter plots in Python with Plotly.

3D scatter plot with plotly express

Like the 2D scatter plot px.scatter, the 3D function px.scatter_3d plots individual data in three-dimensional space. Note that Plotly Express functions take as a first argument a tidy pandas.DataFrame such as the ones defined in px.data.

In [1]:
import plotly.express as px
iris = px.data.iris()
fig = px.scatter_3d(iris, x='sepal_length', y='sepal_width', z='petal_width',
              color='species')
fig.show()

A 4th dimension of the data can be represented thanks to the color of the markers. Also, values from the species column are used below to assign symbols to markers.

In [2]:
import plotly.express as px
iris = px.data.iris()
fig = px.scatter_3d(iris, x='sepal_length', y='sepal_width', z='petal_width',
                    color='petal_length', symbol='species')
fig.show()

Style 3d scatter plot

It is possible to customize the style of the figure through the parameters of px.scatter_3d for some options, or by updating the traces or the layout of the figure through fig.update.

In [3]:
import plotly.express as px
iris = px.data.iris()
fig = px.scatter_3d(iris, x='sepal_length', y='sepal_width', z='petal_width',
              color='petal_length', size='petal_length', size_max=18,
              symbol='species', opacity=0.7)

# tight layout
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))

3D Scatter Plot with go.Scatter3d

Basic 3D Scatter Plot

When data are not available as tidy dataframes, it is also possible to use the more generic go.Scatter3D from plotly.graph_objs. Like the 2D scatter plot go.Scatter, go.Scatter3d plots individual data in three-dimensional space.

In [4]:
import plotly.graph_objects as go
import numpy as np

# Helix equation
t = np.linspace(0, 10, 50)
x, y, z = np.cos(t), np.sin(t), t

fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
                                   mode='markers')])
fig.show()

3D Scatter Plot with Colorscaling and Marker Styling

In [5]:
import plotly.graph_objects as go
import numpy as np

# Helix equation
t = np.linspace(0, 20, 100)
x, y, z = np.cos(t), np.sin(t), t

fig = go.Figure(data=[go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=12,
        color=z,                # set color to an array/list of desired values
        colorscale='Viridis',   # choose a colorscale
        opacity=0.8
    )
)])

# tight layout
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
fig.show()

Dash App

Dash is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its source code can easily be deployed to a PaaS.

In [6]:
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-3dscatterplot/", width="100%", height="950px",frameBorder="0")
Out[6]:
In [7]:
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-3dscatterplot/code", width="100%", height="500px",frameBorder="0")
Out[7]:

Reference

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