plotly Table in Python (4 Examples)
Hi! This tutorial will show you how to build an interactive plotly table in the Python programming language.
Here is an overview:
Let’s get into the Python code!
Install & Import plotly & pandas
In order to be able to construct a table in plotly, we will need to install and import the plotly and pandas Python libraries.
So, in your preferred Python programming IDE, run the lines of code below to install and import plotly and pandas:
# install plotly pip install plotly pip install pandas # import plotly import plotly.graph_objects as go import pandas as pd
The Python pandas library is the foremost library for data analysis and DataFrame manipulation in Python. With it, we can easily construct a DataFrame.
So now that we have both plotly and pandas installed and imported into our Python programming environment, we can now use their functions to build a table.
First, though, we will need to create a dataset.
Create Example Dataset
Here, we will create the example dataset that we will use to construct a table in plotly. Run the code below to construct the example dataset:
df = pd.DataFrame({ "Name":["Andy", "Laura", "Chioma", "Alan", "Sam", "Tony"], "Age": [20,30,25,18,22,24], "Gender": ["Male", "Female", "Female", "Male", "Female", "Male"] }) print(df) # Name Age Gender #0 Andy 20 Male #1 Laura 30 Female #2 Chioma 25 Female #3 Alan 18 Male #4 Sam 22 Female #5 Tony 24 Male
With the dataset created, let us now build a plotly table.
Example 1: Build Basic Table
In this first example, we will build a basic plotly table in Python:
fig = go.Figure(data = [go.Table(header = dict(values = list(df.columns), align = "left"), cells = dict(values = [df.Name, df.Age, df.Gender], align = "left"))]) fig
In the above example, we defined a figure using go.Figure() method.
Inside the figure, there is a table specified using go.Table(). The table has two parts: the header and the cells.
The header is configured with the column names from a DataFrame (df.columns
), and the alignment is set to the left.
The cells are filled with values from the three columns of the same DataFrame (df.Name
, df.Age
, and df.Gender
), and their alignment is also set to the left.
Example 2: Style Table
In this next example, we will add some style to the table to change its appearance:
fig = go.Figure(data = [go.Table(header = dict(values = list(df.columns), align = "left", line_color = "black", fill_color = "#62F939"), cells = dict(values = [df.Name, df.Age, df.Gender], align = "left", line_color = "black", fill_color = "#A4FA8C"))]) fig
Here, we added styling to the table to change its appearance. The code is basically the same as in the previous example. We only defined the line_color =
argument and the fill_color =
argument in both the header and cells of the table.
Example 3: Adjust Row and Column Size of Table
In this third example, we will change the sizes of the columns and rows of the table:
fig = go.Figure(data = [go.Table(header = dict(values = list(df.columns), align = "left", line_color = "black", fill_color = "#62F939", height = 50), cells = dict(values = [df.Name, df.Age, df.Gender], align = "left", line_color = "black", fill_color = "#A4FA8C", height = 40))]) fig
As you can see, adjusting the row and column sizes of the table is as simple as defining the height =
argument in both the header and cells of the table.
Example 4: Change Font Size & Color in Table
In this final example, we will demonstrate how to change the table font size and color:
fig = go.Figure(data = [go.Table(header = dict(values = list(df.columns), align = "left", line_color = "black", fill_color = "#62F939", height = 50, font = dict(color = "red", size = 20)), cells = dict(values = [df.Name, df.Age, df.Gender], align = "left", line_color = "black", fill_color = "#A4FA8C", height = 40, font = dict(color = "blue", size = 20)))]) fig
In the example above, we have changed the color and size of the table font. We only needed to pass a dictionary containing the font color and size to the font =
argument in both the table header and cells.
You can definitely play around with these parameters to further modify your plotly table.
Video, Further Resources & Summary
Do you need more explanations on how to build a plotly table in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain how to build a plotly table in Python.
The YouTube video will be added soon.
You can check out these other articles for more detailed examples and videos of these popular charts in plotly using the Python programming language:
- plotly Map in Python (3 Examples)
- plotly Heatmap in Python (3 Examples)
- plotly Treemap in Python (3 Examples)
- plotly Pie & Donut Chart in Python (4 Examples)
- How to Draw Histograms with plotly in Python (Example)
- Introduction to Python Programming
This post has shown how to build a plotly table in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter