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 you to create and customize various types of charts, plots, and graphs with full control over the visual aspects and interactivity.
The following are some key features of scatter plots using Plotly Graph Objects:
Data visualization: Scatter plots allow you to display individual data points as markers on a two-dimensional plane. They effectively visualize the distribution, relationships, and patterns within your data.
Customization options: You can customize various aspects of the scatter plot, including marker colors, sizes, shapes, and opacity. This enables you to differentiate data points and highlight specific trends.
Hover text: Scatter plots can include hover text that provides additional information about each data point when users hover over them. This feature enhances the interactivity and data exploration experience.
Subplots: Plotly Graph Objects support the creation of subplots, allowing you to arrange multiple scatter plots (or other types of plots) within a single figure. This is useful for comparing different datasets or visualizing multiple variables simultaneously.
Trend lines and regression: You can add trend lines, regression lines, or best-fit lines to scatter plots to visualize trends and relationships more clearly.
Annotations: Scatter plots support annotations, which enable you to add text labels, arrows, or shapes to highlight specific points or regions of interest in the plot.
Interactive features: Plotly scatter plots are interactive by default. Users can zoom, pan, and hover over data points for detailed information. You can also customize interactivity by adding click events to data points.
Axis customization: Customize the appearance of axes, including labels, titles, and scales, to provide context and clarity to your scatter plot.
Layout control: Plotly Graph Objects allow you to control the layout of the entire figure, including titles, legends, and grid settings.
Export and sharing: You can export scatter plots as static images or interactive web-based visualizations. Plotly provides options for saving plots in various formats and sharing them online.
Integration: Scatter plots created with Plotly Graph Objects can be easily integrated into web applications, notebooks, dashboards, and reports.
The scatter plot syntax typically follows this structure:
import plotly.graph_objects as goscatter_plot = go.Figure(go.Scatter(x=x, y=y, mode='markers'))
Following are the key parameters for creating a scatter plot using Plotly Graph Objects:
x
: X-axis data (list or array)
y
: Y-axis data (list or array)
mode
: Determines how data points are displayed ('markers'
, 'lines'
, 'lines+markers'
, 'markers+lines'
, or 'text'
)
name
: Name of the trace for legends
marker
: Dictionary for specifying marker style (e.g., color, size, symbol)
line
: Dictionary for specifying line style (e.g., color, width)
text
: Text labels associated with each data point
hoverinfo
: Specifies what information appears on hover ('x'
, 'y'
, 'text'
, 'name'
, or combinations)
hovertext
: Hover text labels for each data point
hovertemplate
: Template for formatting hover text
showlegend
: Whether to show this trace in legends
opacity
: Opacity of the trace (0 to 1)
fill
: Fill type between points ('none'
, 'tozeroy'
, 'tozerox'
, 'tonexty'
, 'tonextx'
, 'tonext'
)
fillcolor
: Fill color if fill is set
textposition
: Position of text labels relative to markers ('top'
, 'bottom'
, 'middle'
, 'inside'
, or 'none'
)
line_shape
: Line shape between points ('linear'
, 'spline'
, 'hv'
, 'vh'
, 'hvh'
, 'vhv'
, or 'linear spline'
)
The return type of creating a scatter plot using Plotly Graph Objects in Python is an instance of the plotly.graph_objs._figure.Figure
class. This instance represents the complete figure containing the scatter plot, along with any additional traces, layouts, annotations, and configurations you’ve applied to the plot. This return type allows you to further customize and manipulate the plot before displaying or saving it.
In the following playground, we create a scatter plot using a sample dataset called Iris, which is provided by Plotly Express. Used attributes (sepal_width
and sepal_length
) defined as follows:
sepal_length
: It represents the length of the sepal, which is the outer part of the flower that protects the petals. It’s typically measured in centimeters.
sepal_width
: It represents the width of the sepal, measured in centimeters. It’s the measurement taken at the widest part of the sepal.
cd /usercode && python3 main.py python3 -m http.server 5000 > /dev/null 2>&1 &
Note: The above code generates a Plotly scatter plot illustrating the correlation between sepal width and sepal length in the Iris dataset.
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–17: Create a scatter trace using go.Scatter
, specifying the x
and y
data from the dataset columns sepal_width
and sepal_length
. The mode
is set to 'markers'
, indicating that individual data points will be plotted as markers. The name
is set to Sepal
for use in legends.
Line 20: Create a new figure (go.Figure
) and add the previously created scatter trace to it.
Line 23: Update the layout of the figure by setting its title.
Line 26: Display the finalized scatter plot figure using the show()
method.
Plotly Graph Objects provides a streamlined pathway to crafting interactive scatter plots in Python. Its flexibility and customization options empower users to succinctly visualize data relationships and patterns. The dynamic capabilities of Plotly Graph Objects streamline the process of creating impactful scatter plots, enhancing data exploration and communication.
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