.scatter_3d()
The .scatter_3d()
method in the plotly.express
module creates a 3D scatter plot to visualize the relationships between three variables using markers in a three-dimensional space. The data points are plotted based on their values on the x
, y
, and z
axes. It also allows customizing marker colors, sizes, and symbols.
Syntax
The x
, y
, and z
parameters are required and accept values as a string, integer, Series
, or array-like object, representing the data for each axis. Optional parameters, such as color
, symbol
, and size
, customize the markers’ appearance. If data_frame
is not provided, a DataFrame
will be constructed from other arguments.
plotly.express.scatter_3d(data_frame=None, x=None, y=None, z=None, color=None, symbol=None, size=None, ...)
data_frame
: The PandasDataFrame
containing the data to visualize.x
: The column name indata_frame
,Series
, or array-like object for x-axis data.y
: The column name indata_frame
,Series
, or array-like object for y-axis data.z
: The column name indata_frame
,Series
, or array-like object for z-axis data.color
: The column name indata_frame
,Series
, or array-like object to specify marker colors.symbol
: The column name indata_frame
,Series
, or array-like object for assigning marker symbols.size
: The column name indata_frame
,Series
, or array-like object to assign marker sizes.
Note: The ellipsis in the syntax (…) indicates additional optional parameters that can be used to customize the plot further.
Examples
The examples below demonstrate how the .scatter()
method creates a 2D scatter plot, while the .scatter_3d()
method provides a more complex visualization in three dimensions, utilizing additional parameters for color and symbol customization.
Example 1
The following example demonstrates the use of the .scatter()
method:
# Defining 'x' and 'y' as array-like objectsimport plotly.express as pxx = [1, 3, 5, 7, 9]y = [4, 6, 5, 8, 2]# Creating a scatter plotfig = px.scatter(x = x, y = y)# Displaying the plotfig.show()
The output for the above code is as follows:
Example 2
This example illustrates the use of the .scatter_3d()
method, showcasing its capability to utilize more parameters for enhanced visualization:
import plotly.express as px# Sample Datax = [1, 3, 5, 7, 9]y = [4, 6, 5, 8, 2]z = [7, 2, 9, 4, 8]color = ['red', 'green', 'blue', 'purple', 'orange']symbol = ['circle', 'square', 'diamond', 'star', 'triangle-up']# Creating a 3D scatter plotfig = px.scatter_3d(x=x, y=y, z=z, color=color, symbol=symbol, title="3D Scatter Plot with Colors and Symbols")# Displaying the plotfig.show()
The above code produces the following output:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Plotly on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours