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
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.
The scatter_3d
function syntax typically follows this structure:
import plotly.express as pxfig = px.scatter_3d(data_frame, x='x_column', y='y_column', z='z_column', ...)
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
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
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.
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.
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 &
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 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.
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 and its methods
Learn the core concepts of Plotly Graph Objects, including its structure, methods, and how to create fully customized visualizations.
Creating a density heatmap plot with Plotly Express in Python
Learn to visualize data density using heatmaps, making patterns in large datasets easy to interpret.
How to create a line plot with Plotly Express in Python
Master the basics of line plots to represent trends over time and relationships between variables.
How to create a bar plot with Plotly Express in Python
Understand how to create bar plots to compare categorical data effectively.
How to create a histogram with Plotly Express in Python
Explore histograms to analyze data distribution and frequency counts efficiently.
How to create a box plot with Plotly Express in Python
Learn to use box plots for statistical visualization, identifying outliers and data spread.
How to create a violin plot with Plotly Express in Python
Combine box plots and KDE plots to compare data distributions effectively.
How to create a 3D line plot with Plotly Express in Python
Extend your data visualization skills by creating 3D line plots for multi-dimensional data representation.
How to create a choropleth map with Plotly Express in Python
Learn how to create geospatial visualizations with choropleth maps for regional data analysis.
Creating parallel coordinates plots with Plotly Express in Python
Visualize multi-dimensional data efficiently with parallel coordinate plots for feature comparison.
How to create a scatter plot on a Mapbox map with Plotly Express
Utilize Mapbox maps to plot scatter data points based on geographic coordinates.
Creating a scatter plot matrix with Plotly Express in Python
Understand relationships between multiple numerical variables using scatter plot matrices.
Plotly Graph Objects: Customization and advanced features
How to create a 3D surface plot with Plotly Graph Objects
Create 3D surface plots for visualizing complex surfaces and mathematical functions.
How to create a box plot with Plotly Graph Objects in Python
Gain full control over box plots, including styling, custom axes, and multiple data series.
How to create a 3D scatter plot with Plotly Express in Python
Visualize high-dimensional data using 3D scatter plots for better insight.
Creating a histogram plot with Plotly Graph Objects in Python
Customize histogram bins, colors, and overlays using Plotly Graph Objects for in-depth analysis.
How to create a bar plot with Plotly Graph Objects in Python
Build highly customizable bar plots, adjusting layout, colors, and interactivity.
How to create a heatmap plot with Plotly Graph Objects in Python
Generate heatmaps with flexible color scales and annotations for better data storytelling.
How to create a pie plot with Plotly Graph Objects in Python
Learn to create pie charts with custom labels, colors, and hover interactions.
Creating a Choropleth plot with Plotly Graph Objects in Python
Explore geospatial visualizations with advanced choropleth maps for regional comparisons.
How to create a violin plot with Plotly Graph Objects in Python
Customize violin plots to represent distribution, density, and probability density functions.
How to create a scatter plot with Plotly Graph Objects in Python
Learn to create scatter plots with detailed hover information, styling, and annotations.
How to create a table with Plotly Graph Objects in Python
Build interactive tables with styling options for presenting structured data.
How to create a bubble plot with Plotly Graph Objects in Python
Understand how to create bubble plots to visualize three variables in a single chart.
Create a 3D scatter plot with Plotly Graph Objects in Python
Explore multi-dimensional data using customized 3D scatter plots.
Creating a density contour plot with Plotly Express in Python
Learn how to visualize data density using contour plots to detect clusters.
How to create a scatter plot with Plotly Express in Python
Master scatter plots to identify correlations, trends, and patterns in datasets.
Free Resources