How to create a 3D scatter plot with Plotly Express in Python

Plotly Express is a Python library that allows us to create line plots quickly and easily, with customizable parameters and an interactive interface.


A 3D scatter plot displays data points in a three-dimensional coordinate system, where each data point is represented by its position in the xx, yy, and zz dimensions.

Features of the 3D scatter plot

Some of the key features of 3D scatter plot include:

  • Interactive rotation: Users can rotate the plot in 3D space to view it from different angles, providing a better understanding of the data distribution.

  • Zooming and panning: The plot can be zoomed in and out, allowing users to focus on specific regions of interest. Panning enables navigation across the plot.

  • Hover information: When hovering over a data point, users can see additional information about that specific point, such as its coordinates or any other associated data.

  • Marker customization: Users can customize the appearance of the markers representing data points. This includes changing the marker shape, size, color, opacity, and more.

  • Axis labels and titles: Axes can be labeled to provide context and meaning to the dimensions being plotted. The plot can also have a title to give an overall description or purpose.

  • Color scaling: Data points can be colored based on a specific dimension or a numerical value, allowing for better visualization of trends or patterns.

  • Size scaling: Marker size can be scaled based on a specified column in the dataset, making it possible to represent additional information visually.

  • Animation: If the dataset contains a temporal component, Plotly Express can create an animated 3D scatter plot that shows the evolution of the data over time.

  • Scene configuration: Plotly Express provides options to configure the scene layout, including the camera position, lighting conditions, and background color.

  • Exporting and sharing: The generated 3D scatter plots can be exported as static images or interactive HTML files. These files can be shared or embedded in websites or notebooks for easy dissemination.

Syntax

The scatter_3d function syntax typically follows this structure:

import plotly.express as px
fig = px.scatter_3d(data_frame, x='x_column', y='y_column', z='z_column', ...)
Syntax of the scatter_3d function

Parameters

The followings are the parameters of scatter_3d function:

  • data_frame: This is the pandas DataFrame or data structure containing the data to be plotted.

  • x, y, z: These are strings or list-like objects representing the columns in the data_frame that correspond to the xx, yy, and zz dimensions, respectively.

  • color: This specifies how the markers are colored. It can take the column name or a numerical array-like object. We can also use categorical columns or assign a fixed color value.

  • symbol: This specifies the marker symbol. It can be a string representing a predefined symbol or a column name with categorical values.

  • size: This determines the marker size. It can be a numerical column name or a constant value.

  • opacity: This sets the marker opacity (0-1). It can be a column name or a constant value.

  • hover_name: This specifies the column name to be displayed when hovering over a data point.

  • hover_data: These are additional columns from the data_frame to display in the hover tooltip. It can be a list of column names.

  • title: This sets the title of the plot.

  • labels: This is a dictionary specifying the labels for the xx, yy, and zz axes.

  • range_x, range_y, range_z: This specifies the range of values to display on the respective axes.

  • animation_frame: This allows plot animation based on a column representing the temporal dimension.

  • template: This specifies the template theme to be used for the plot.

Return type

The px.scatter_3d() function returns a Plotly figure object that can be displayed with fig.show(). The figure object contains all the information required to produce the 3d scatter plot, including the data, layout, and style.

Implementation

In the following playground, we create a 3D scatter plot map using a sample dataset called "iris" provided by Plotly Express. Used attributes ('sepal_width', 'sepal_length', and 'petal_length') defined as follows:

  • "sepal_width": This is the measurement of the width of the sepal, which is the leaf-like structure that protects the flower bud. It is given in centimeters (cm).

  • "sepal_length": This is the measurement of the length of the sepal, which is the leaf-like structure that protects the flower bud. It is given in centimeters (cm).

  • "petal_length": This is the measurement of the length of the petal, which is the colorful leaf-like structure of the flower. It is given in centimeters (cm).

cd /usercode && python3 main.py
python3 -m http.server 5000 > /dev/null 2>&1 &
Create a 3D scatter plot of the iris dataset

Explanation

The code above is explained in detail below:

  • Lines 2–3: We import the required libraries for the code: plotly.express as px for creating the 3D scatter plot, and pandas as pd for handling data in a DataFrame.

  • Line 6: We load the Plotly iris dataset using the px.data.iris() function and assigns it to the variable df. The df variable will now hold the dataset, allowing us to access and analyze its contents.

  • Line 9: We print the first five rows of the loaded dataset. The head() function retrieves the top rows of the DataFrame and print() displays the result in the console. It helps to quickly inspect the data and verify its structure.

  • Line 12: We create a 3D scatter plot. The scatter_3d takes the DataFrame df as the data source and specifies the columns to use for the xx, yy, and zz dimensions (sepal_width, sepal_length, and petal_length, respectively). The color parameter assigns different colors to data points based on the species column.

  • Line 23: We display the plot using the fig.show() method, which shows the interactive plot.

Conclusion

Plotly Express offers a powerful and intuitive way to create 3D scatter plots. With just a few lines of code, we can visualize and explore data points in three-dimensional space. The ability to assign colors and customize the plot based on different variables provides additional insights into the relationships between multiple dimensions. Whether it's for data analysis, visualization, or machine learning tasks, Plotly Express's 3D scatter plots enable users to effectively convey complex information and uncover patterns in their data. Its user-friendly interface and extensive customization options make it a valuable tool for visualizing multidimensional data.

Unlock your potential: Plotly Graphing and Visualization series, all in one place!

To deepen your understanding of data visualization using Plotly, explore our comprehensive Answer series below:

Plotly express: quick and intuitive visualization

Plotly Graph Objects: Customization and advanced features

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved