How to create a table 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 allow us to create and customize various types of charts, plots, and graphs with full control over the visual aspects and interactivity.

Features of the table in Plotly graph objects

The following are some key features of table plots using Plotly graph objects:

  • Data input: This creates tables by providing data in a structured format, including headers and rows of values.

  • Header row: This defines a header row to specify column names or labels for the table.

  • Cell data: This populates the table with data cells containing information or values.

  • Cell formatting: This customizes the appearance of individual cells, such as font size, color, and alignment.

  • Column widths: This specifies the width of individual columns to control the table’s layout.

  • Text and font properties: This customizes text properties, including font size, color, and style, for headers and cells.

  • Horizontal and vertical alignment: This aligns text within cells horizontally and vertically to achieve the desired layout.

  • Header and cell background color: This sets background colors for header and data cells to enhance visual appeal.

  • Sorting: This enables table data sorting based on specific columns, allowing users to interactively organize the information.

  • Column and row groups: This groups columns or rows together to create a structured and organized table layout.

  • Styling: This customizes the table’s overall appearance, including borders, cell spacing, and other visual elements.

  • Interactive features: Tables in Plotly can be interactive, allowing users to click cells, rows, or columns for additional actions or information.

  • Responsive design: Plotly tables are designed to be responsive, adapting to the size of the container or screen on which they are displayed.

  • Text and pronouns: This uses appropriate pronouns and gender-neutral language in the table content.

  • Superlatives and opinions: This avoids making extreme superlative statements about data or content within the table.

  • Appropriate examples: This ensures that any examples or data used in the table suit a global audience and avoid potentially controversial topics.

  • Accessible language: This uses clear and accessible language in the table’s content to accommodate non-native speakers and avoid idiomatic expressions.

Syntax

The table plot syntax typically follows the structure given below:

import plotly.graph_objects as go
table_plot = go.Figure(data=[go.Table(header=header, cells=cells)])
Syntax of the table plot

Parameters

The following are the key parameters for creating a table plot using Plotly graph objects:

  • header: A dictionary specifying the header row of the table, which typically includes column names or labels

  • cells: A dictionary specifying the data cells of the table as a list of lists, where each list represents a row of data

  • columnwidth: A list specifying the width of individual columns to control the table’s layout

  • columnorder: A list specifying the order in which columns appear in the table

  • header_align: A list specifying the horizontal alignment of header cells

  • header_line_color: The color of the lines separating header cells

  • fill_color: A list specifying the background color for each cell or row

  • font: A dictionary specifying text font properties for header and cell text, including size, color, and family

  • align: A list specifying the horizontal alignment of cell text

  • valign: A list specifying the vertical alignment of cell text

  • line_color: The color of the lines separating data cells

  • line_width: The width of the lines separating data cells

  • cell_pattern: A list specifying a pattern for cell borders

  • visible: A boolean parameter to control the visibility of the table plot

  • row_height: The height of individual rows in the table

  • column_width: The width of individual columns in the table

  • customdata: Additional data associated with the table, which can be used for interactive features

  • hoverinfo: A string specifying what information to display when hovering over the table

  • hovertemplate: A template for customizing the hover text

  • domain: A dictionary specifying the position and size of the table within the plot

  • header_pattern: A list specifying a pattern for header borders

  • column_pattern: A list specifying a pattern for column borders

  • row_pattern: A list specifying a pattern for row borders

  • header_fill: A list specifying the background color for header cells

  • header_values: A list specifying custom values for header cells

  • ids: A list of unique identifiers for rows

  • cells_fill: A list specifying the background color for data cells

  • customdatasrc: A string specifying the source of custom data

  • idsrc: A string specifying the source of row identifiers

  • rowssrc: A string specifying the source of row data

  • columnordersrc: A string specifying the source of column order data

Return type

When we create a visualization like a table plot using the go.Table trace and the go.Figure constructor in Plotly graph objects, we are essentially creating a figure object that represents our table plot. The figure object contains all the necessary information about our table plot, including the data, table trace, layout, and any additional settings or customizations we’ve applied.

Implementation

In the following playground, we create a table plot using a sample dataset called “gapminder” provided by Plotly Express. Attributes of the gapminder dataset (country, continent, year, lifeExp , pop , gdpPercap and species) are defined as follows:

  • country: The name of the country for the data point

  • continent: The continent to which the country belongs

  • year: The year in which the data was recorded

  • lifeExp: The life expectancy of the population in years

  • pop: The population of the country

  • gdpPercap: The gross domestic product (GDP) per capita, which is the economic output per person in USD

  • iso_alpha: The ISO alpha-3 code representing the country

cd /usercode && python3 main.py
python3 -m http.server 5000 > /dev/null 2>&1 &
Visualizing the gapminder dataset as a table plot in Plotly graph objects

Explanation

The code above is explained 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 gapminder 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.

  • Line 12: Define the header dictionary, which specifies the header row for the table plot. It contains column names: Country, Year, and Population.

  • Line 15: Define the cells dictionary, which specifies the data cells for the table plot. It contains columns from the df dataset: country, year, and pop, representing the corresponding data for each column.

  • Line 18: Create a table plot using Plotly graph objects. The go.Figure constructor is used to create a figure object (table_plot) with the table data specified in the header and cells dictionaries.

  • Line 21–26: Customize the appearance of the table plot using the update_layout method. Several layout parameters are set, including the title, width, height, and margin, to control the table’s visual presentation.

  • Line 29: Display the finalized table plot figure using the show() method.

Conclusion

The use of table plots in Plotly graph objects offers a powerful tool for visualizing and presenting tabular data. By combining the flexibility of Plotly with the structured format of tables, we can create informative and interactive displays of data that are well-suited for various applications. Whether we need to showcase data summaries, comparisons, or detailed information, Plotly’s table plots allow for customization and interactivity, making them a valuable asset in the data visualization toolkit. With the ability to format, style, and tailor our tables to specific requirements, Plotly graph objects empowers us to communicate data effectively and engage our audience in a meaningful way.

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