Tables#
plotly.express
does not include a table function, but we can create a graph object table using go.Figure()
in combination with go.Table()
. We can create two columns of data with sample scores for A
and B
letter grades.
import plotly.graph_objects as go # import statement
# create figure with data
fig = go.Figure(data=[go.Table(header=dict(values=['A Scores', 'B Scores']),
cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))
])
# show figure
fig.show()
There’s a lot to get into in terms of the differences between plotly.express
function syntax and plotly.graph_object
syntax.
For our purposes, we can focus on how the table header takes a dictionary with column labels, and the cells also take a dictionary with two lists of values. These dictionaries are passed to go.Figure()
and go.Table()
to create the plot.
DataFrames & Tables#
We could also generate a table from data stored in a pandas
DataFrame
.
This example also includes style parameters for the table.
import pandas as pd # import statement
df = pd.read_csv('https://raw.githubusercontent.com/kwaldenphd/elements-of-computing/main/book/data/ch11/2014_usa_states.csv') # load data
df # inspect data
# create figure
fig = go.Figure(data=[go.Table(
header=dict(values=list(df.columns),
fill_color='paleturquoise',
align='left'),
cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],
fill_color='lavender',
align='left'))
])
# show figure
fig.show()
This example includes style attributes like fill_color
and align
for both header
and cells
.
For header
, this example takes a dictionary with the DataFrame
column labels as a list, and the DataFrame
column values as values for the cells
.