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 types of charts, plots, and graphs with full control over the visual aspects and interactivity.
The following are some key features of box plots using Plotly Graph Objects:
Customizable appearance: Plotly GO allows us to customize various aspects of the box plot, such as colors, line styles, fill colors, etc.
Multiple box plots: We can create a grouped box plot or a box plot with multiple boxes for different categories or groups by providing data for each group in the trace.
Horizontal box plots: We can create horizontal box plots by setting the orientation
parameter to 'h'
.
Box plot outliers: We can choose to display or hide outliers using the boxpoints
parameter. Options include 'outliers'
, 'suspectedoutliers'
, 'all'
, or False
.
Box plot notches: Notches on the sides of the box can provide a confidence interval around the median. We can control the display of notches using the notched
parameter.
Customizable axes labels and titles: We can customize the labels, titles, and ranges of the x and y axes using various parameters.
Hover labels and tooltips: Plotly GO supports interactive features like hover labels and tooltips, which allow us to display additional information when hovering over the boxes.
Styling and layout: Plotly GO provides options to customize the overall layout of the plot, including margins, title, legend, and grid lines.
Subplots: We can create subplots with multiple box plots arranged in rows or columns using the make_subplots
function.
Themes and templates: Plotly GO allows us to apply predefined themes or custom templates to ensure consistent styling across our plots.
The box plot syntax typically follows this structure:
import plotly.graph_objects as gobox_plot = go.Figure(go.Box(y=data, name='Box Plot'))
The following are the key parameters for creating a box plot using Plotly Graph Objects:
y
: A list of numerical values representing the data for the y-axis.
x
: A list of numerical values representing the data for the x-axis (for horizontal box plots).
name
: A string name for the trace, which will be displayed in the legend.
boxpoints
: Determines which data points are shown as individual points in the plot. It can take the options: 'outliers'
, 'suspectedoutliers'
, 'all'
, and False.
boxmean
: If 'sd'
, it displays the mean ± standard deviation as a dashed line inside the box.
notched
: If True
, it creates a notched box plot. If False
, it creates a rectangular box plot.
boxgap
: Sets the gap (in pixels) between boxes.
boxgroup
: A string that specifies the group of boxes that will be treated as a single box when hovered over.
boxmode
: Determines how boxes at the same location coordinate are displayed on the graph. It can take one of two options: 'group'
or 'overlay'
.
marker
: A dictionary of marker properties, including 'color'
, 'line'
, and more.
line
: A dictionary of line properties, including 'color'
, 'width'
, and more.
text
: A list or array of text labels associated with the data points.
hoverinfo
: Determines what information appears on hover. Options include 'x'
, 'y'
, 'text'
, 'name'
, and more.
orientation
: If 'h'
, it creates a horizontal box plot.
name
: A string that sets the name of the trace.
showlegend
: Determines whether the trace is shown in the legend.
xaxis
and yaxis
: Dictionaries of axis properties for the x-axis and y-axis, respectively.
When we create a visualization like a box plot using the go.Box
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 box plot using a sample dataset called iris that is provided by Plotly Express. The used attributes (sepal_width
and species
) are 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 is typically measured in centimeters.
species
: This refers to the different types of iris flowers (setosa, versicolor, and virginica).
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: We import the necessary modules: plotly.graph_objects
for creating custom plots, plotly.express
for simplified plotting, and pandas
for data manipulation.
Line 6: We load the iris dataset using Plotly Express’s built-in sample dataset.
Line 9: We print the first five rows of the loaded dataset using the head()
method to inspect the data.
Lines 12–16: We create a box plot trace using Plotly Graph Objects. The x
parameter represents the different species, y
represents the sepal length, and name
gives a label to the trace.
Line 19: We create a figure using the go.Figure()
constructor and add the previously created box plot trace.
Line 22: We update the layout of the figure by setting its title.
Line 25: We display the finalized box plot figure using the show()
method.
Plotly Graph Objects provides a powerful and flexible framework for creating insightful box plots. Its seamless integration with Plotly Express and extensive customization options empower users to craft interactive and visually appealing box plots that effectively convey data distribution and statistical characteristics across various categories or groups. With the ability to fine-tune aesthetics, explore outliers, and present summary statistics, Plotly Graph Objects enhances the data visualization experience, making it an essential tool for researchers, analysts, and data scientists aiming to gain deeper insights from their datasets.
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