How to create a box plot with Plotly Graph Objects in Python

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.

Features of the box plot in Plotly Graph Objects

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.

Syntax

The box plot syntax typically follows this structure:

import plotly.graph_objects as go
box_plot = go.Figure(go.Box(y=data, name='Box Plot'))
Syntax of the box plot

Parameters

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.

Return type

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.

Implementation

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 &
Visualizing the distribution of sepal lengths across iris species with plotly Graph objects

Explanation

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.

Conclusion

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: Customization and advanced features

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved