HOWTO · Plotly

How to Create a 3D Scatter Plot in Plotly

Create a 3D scatter plot in Python with Plotly Express scatter_3d(), then customize colors, hover labels, axes, and traces.

On this page

Use Plotly Express and scatter_3d() to map three columns of a data frame to the x, y, and z dimensions of an interactive 3D scatter plot. The example below loads Plotly’s built-in Iris data and colors points by species; call fig.show() in an interactive environment to open the figure in its configured renderer.

Use the scatter_3d() Function of Plotly to Create a 3D Scatter Plot in Python

Each row in the data frame supplies one point. Set x, y, and z to column names containing the values for the three plotted dimensions. Plotly Express also accepts arrays for these arguments, but using columns keeps each point’s values and labels together.

This complete example uses the Iris measurements and species labels included with Plotly Express. Install Plotly in your Python environment if it is not already available. It builds the figure and prints a compact summary; call fig.show() in an interactive Python environment to open the chart.

import plotly.express as px

df = px.data.iris()
fig = px.scatter_3d(
    data_frame=df,
    x="sepal_length",
    y="sepal_width",
    z="petal_width",
    color="species",
    title="Iris measurements",
)
print(f"Points: {len(df)}")
print(f"Traces: {len(fig.data)}")
for trace in fig.data:
    print(f"{trace.name}: {len(trace.x)} points")
Points: 150
Traces: 3
setosa: 50 points
versicolor: 50 points
virginica: 50 points

The color="species" setting maps the categorical values in the species column to distinct colors; it does not assign random colors. For numeric data, mapping color to a numeric column uses a continuous scale. size can map a numeric column to marker size, while hover_name selects a column shown prominently when a reader points to a marker. Use hover_data to add or suppress other fields in the hover label. The optional text argument can show a column’s value beside each point, though labels may overlap in a dense plot.

Pass the data frame as the first argument or as data_frame. If you have only one measurement column, choose meaningful columns for the other two dimensions as well: do not assume the missing dimensions will be filled with row indices. The selected columns should contain values suitable for plotting in three dimensions.

Change Color and Traces of the 3D Scatter Plot in Python

For categorical colors, color_discrete_sequence chooses the palette, for example px.colors.qualitative.Dark24. Use color_discrete_map to assign specific colors to category names; categories absent from that mapping use the remaining default color behavior. See Plotly’s discrete color documentation for palette and mapping details. Set opacity between 0 and 1 to make markers translucent, which can help reveal overlapping points.

You can add error bars by passing data-frame columns to error_x, error_y, or error_z; each column supplies the corresponding dimension’s error values. The Iris data used above has no error columns, so provide suitable error data in your own frame before using these arguments. To display a logarithmic scale on a dimension, set its matching log_x, log_y, or log_z option to True. Logarithmic scales require positive values. Set a figure title with title; use fig.update_layout(showlegend=False) to hide the legend.

Plotly Express returns a figure containing one or more traces, commonly one trace per species when color is categorical. Call fig.update_traces(...) to apply a trace-level setting to the traces, or pass a selector such as selector=dict(name="setosa") to target a particular trace. For example, fig.update_traces(marker_opacity=0.6) changes marker opacity across traces. Trace properties and supported error-bar settings are documented in Plotly’s scatter3d reference.