Plotly Graph Objects is a Python library that provides a flexible and powerful way to create interactive data visualizations. It is part of the larger Plotly ecosystem, which includes Plotly Express and Plotly.py. Plotly Graph Objects allows us to create and customize various charts, plots, and graphs with full control over the visual aspects and interactivity.
The following are some key features of 3D surface plots using Plotly Graph Objects:
Data input: We can create 3D surface plots by providing data as a 2D grid of values. Our data can be a NumPy
array, a pandas
DataFrame, or a list of lists.
Surface plot: We typically use the go to create a 3D surface plot. Surface trace type in Plotly.
x, y, and z axes: We can specify the
Color and color scale: We can map the values of the
Opacity: We can control the opacity of the surface plot using the opacity attribute. Depending on our visualization needs, this can make the plot more or less transparent.
Contours: We can add contour lines to our surface plot using the contours attribute. This allows us to visualize the 2D projections of the surface plot on the
Lighting and shading: Plotly allows us to customize the lighting and shading of the 3D surface plot. We can adjust parameters like lighting.ambient
, lighting.diffuse
, and lighting.specular
, to control how the plot is illuminated.
Color bar: We can include a color bar alongside our 3D surface plot to indicate the correspondence between colors and data values. The color bar attribute provides control over its appearance and placement.
Titles and labels: We can add titles and labels to the plot and axes to provide context and clarity to our visualization. We use the title, xaxis.title
, yaxis.title
, and zaxis.title
attributes to set titles.
Scene: In a 3D plot, we can define the layout and appearance of the scene. This includes setting the camera position, aspect ratio, and more through the scene attribute.
Annotations: We can add annotations to our 3D surface plot to highlight specific data points or regions. Annotations can include text labels, lines, or shapes.
Interactive features: Plotly 3D surface plots are interactive by default. Users can pan, zoom, and rotate the plot to explore it from different angles.
Export options: We can save our 3D surface plot as an image (e.g., PNG or JPEG) or as an interactive HTML file.
The 3D surface plot syntax typically follows this structure:
import plotly.graph_objects as gosurface_trace = go.Surface(x=x, # x-axis datay=y, # y-axis dataz=z, # z-axis datacolorscale='Viridis', # Color scalecontours=dict(z=dict(show=True, highlightwidth=0.1)), # Contour lines (optional))
The following are the key parameters for creating a 3D surface plot using Plotly Graph Objects:
x
: The
y
: The
z
: The
colorscale
: Specifies the color scale used to map the Viridis
, Jet
, Rainbow
, or create custom color scales.
contours
: An optional dictionary that allows us to configure contour lines on the surface plot. We can control the display of contour lines using attributes like show
, coloring
, and highlightwidth
.
opacity
: Sets the opacity of the surface. It's a value between 0 (completely transparent) and 1 (completely opaque).
cmin
and cmax
: These parameters allow us to specify the minimum and maximum values for the color scale. Values outside this range will be mapped to the endpoints of the color scale.
lighting
: A dictionary that controls the lighting of the surface plot. We can adjust parameters like ambient
, diffuse
, and specular
to customize how light interacts with the plot's surface.
colorbar
: Allows us to customize the colorbar associated with the surface plot. We can set attributes like title
, titleside
, and tickvals
.
hoverinfo
: Specifies what information is displayed when hovering over data points on the plot. We can include information about X, Y, Z values, or customize it further.
scene
: A dictionary that configures the layout and appearance of the 3D scene. We can set attributes like xaxis_title
, yaxis_title
, zaxis_title
, and camera
(for adjusting the view angle).
showscale
: Determines whether or not to display a color scale alongside the plot.
name
: The name of the trace, which can be useful when working with multiple traces in the same figure.
reversescale
: Specifies whether to reverse the color scale.
surfacecolor
: Allows us to set a constant color for the entire surface.
coloraxis
: A dictionary to control how color scaling is applied. We can set attributes like colorbar
, colorbar_title
, and colorscale
.
When we create a visualization like a 3D surface plot using the go.Surface
trace and the go.Figure
constructor, we are essentially creating a figure object that represents our plot. The figure object contains all the necessary information about our plot, including the data, traces, layout, and any additional settings or customizations we've applied.
In the following playground, we create a 3D surface plot using a sample dataset called iris provided by Plotly Express. Used attributes (sepal_width
, sepal_length
and petal_width
) defined as follows:
sepal_width
: This attribute represents the width of the sepals of iris flowers. Sepals are the leaf-like structures that protect the flower's bud before it blooms. Sepal width is typically measured in centimeters (cm).
sepal_length
: This attribute represents the length of the sepals of iris flowers. Like sepal width, it is also measured in centimeters (cm). Sepal length provides information about the size and shape of the sepals.
petal_width
: This attribute represents the width of the petals of iris flowers. Petals are the colorful, leaf-like structures inside a flower that are involved in attracting pollinators. Petal width is measured 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 1–3: Import the necessary modules: plotly.graph_objects
for creating custom plots, plotly.express
for simplified plotting, and pandas
for data manipulation.
Line 6: Load the Iris
dataset using Plotly Express's built-in sample dataset.
Line 9: Print the first five rows of the loaded dataset using the head()
method to inspect the data.
Lines 12–16: A 3D surface plot trace is created using Plotly's graph_objects. The z-axis (vertical) is assigned the petal_width
column from the dataset, the x-axis is assigned sepal_width
, and the y-axis is assigned sepal_length
.
Line 19: This line creates a new figure using Plotly's graph_objects and adds the 3D surface plot trace surface_trace
to it. A figure is a container for plots.
Line 22: Update the layout of the figure by setting its title.
Line 25: Display the finalized 3d surface plot figure using the show()
method.
3D surface plots in Plotly Graph Objects offer a powerful means of visualizing complex, multidimensional data. This capability is particularly valuable for understanding relationships within three dimensions of data points. Plotly's flexible library allows for precise customization, from controlling color scales and contour lines to adjusting lighting and layout. As a result, it empowers data analysts and scientists to create interactive and informative 3D surface plots that reveal insights, patterns, and trends in their data, making it a valuable tool in the field of data visualization and analysis.
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