Figure Factory Tables in Python

How to make tables in Python with Plotly's Figure Factory.


New to Plotly?

Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Tables can be created using a table trace type, or by using a figure factory as detailed in this page.

Simple Table

In [1]:
import plotly.figure_factory as ff

data_matrix = [['Country', 'Year', 'Population'],
               ['United States', 2000, 282200000],
               ['Canada', 2000, 27790000],
               ['United States', 2005, 295500000],
               ['Canada', 2005, 32310000],
               ['United States', 2010, 309000000],
               ['Canada', 2010, 34000000]]

fig = ff.create_table(data_matrix)
fig.show()
CountryYearPopulationUnited States2000282200000Canada200027790000United States2005295500000Canada200532310000United States2010309000000Canada201034000000
In [2]:
import plotly.figure_factory as ff

data_matrix = [['User', 'Language', 'Chart Type', '# of Views'],
               ['<a href="https://plotly.com/~empet/folder/home">empet</a>',
                '<a href="https://plotly.com/python/">Python</a>',
                '<a href="https://plotly.com/~empet/8614/">Network Graph</a>',
                298],
               ['<a href="https://plotly.com/~Grondo/folder/home">Grondo</a>',
                '<a href="https://plotly.com/matlab/">Matlab</a>',
                '<a href="https://plotly.com/~Grondo/42/">Subplots</a>',
                356],
               ['<a href="https://plotly.com/~Dreamshot/folder/home">Dreamshot</a>',
                '<a href="https://help.plot.ly/tutorials/">Web App</a>',
                '<a href="https://plotly.com/~Dreamshot/6575/_2014-us-city-populations/">Bubble Map</a>',
                262],
               ['<a href="https://plotly.com/~FiveThirtyEight/folder/home">FiveThirtyEight</a>',
                '<a href="https://help.plot.ly/tutorials/">Web App</a>',
                '<a href="https://plotly.com/~FiveThirtyEight/30/">Scatter</a>',
                692],
               ['<a href="https://plotly.com/~cpsievert/folder/home">cpsievert</a>',
                '<a href="https://plotly.com/r/">R</a>',
                '<a href="https://plotly.com/~cpsievert/1130/">Surface</a>',
                302]]

fig = ff.create_table(data_matrix)
fig.show()

Use LaTeX

In [3]:
import plotly.figure_factory as ff

data_matrix = [['Name', 'Equation'],
               ['Pythagorean Theorem', '$a^{2}+b^{2}=c^{2}$'],
               ['Euler\'s Formula', '$F-E+V=2$'],
               ['The Origin of Complex Numbers', '$i^{2}=-1$'],
               ['Einstein\'s Theory of Relativity', '$E=m c^{2}$']]

fig =  ff.create_table(data_matrix)
fig.show()
NameEquationPythagorean TheoremEuler's FormulaThe Origin of Complex NumbersEinstein's Theory of Relativity

Use a Pandas Dataframe

In [4]:
import plotly.figure_factory as ff

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
df_sample = df[100:120]

fig =  ff.create_table(df_sample)
fig.show()
countryyearpopcontinentlifeExpgdpPercapBangladesh197270759295.0Asia45.252630.2336265Bangladesh197780428306.0Asia46.923659.8772322Bangladesh198293074406.0Asia50.009676.9818656Bangladesh1987103764241.0Asia52.819751.9794035Bangladesh1992113704579.0Asia56.018837.8101643Bangladesh1997123315288.0Asia59.412972.7700352Bangladesh2002135656790.0Asia62.0131136.39043Bangladesh2007150448339.0Asia64.0621391.253792Belgium19528730405.0Europe68.08343.105127Belgium19578989111.0Europe69.249714.960623Belgium19629218400.0Europe70.2510991.20676Belgium19679556500.0Europe70.9413149.04119Belgium19729709100.0Europe71.4416672.14356Belgium19779821800.0Europe72.819117.97448Belgium19829856303.0Europe73.9320979.84589Belgium19879870200.0Europe75.3522525.56308Belgium199210045622.0Europe76.4625575.57069Belgium199710199787.0Europe77.5327561.19663Belgium200210311970.0Europe78.3230485.88375Belgium200710392226.0Europe79.44133692.60508

Modify Row Height

The default row height is 30 pixels. Set height_constant if you'd like to change the height of each row.

In [5]:
import plotly.figure_factory as ff

data_matrix = [['Country', 'Year', 'Population'],
               ['United States', 2000, 282200000],
               ['Canada', 2000, 27790000],
               ['United States', 2005, 295500000],
               ['Canada', 2005, 32310000],
               ['United States', 2010, 309000000],
               ['Canada', 2010, 34000000]]

fig =  ff.create_table(data_matrix, height_constant=20)
fig.show()
CountryYearPopulationUnited States2000282200000Canada200027790000United States2005295500000Canada200532310000United States2010309000000Canada201034000000

Custom Table Colors

In [6]:
import plotly.figure_factory as ff

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
df_sample = df[400:410]

colorscale = [[0, '#4d004c'],[.5, '#f2e5ff'],[1, '#ffffff']]

fig =  ff.create_table(df_sample, colorscale=colorscale)

fig.show()
countryyearpopcontinentlifeExpgdpPercapCzech Republic19729862158.0Europe70.2913108.4536Czech Republic197710161915.0Europe70.7114800.16062Czech Republic198210303704.0Europe70.9615377.22855Czech Republic198710311597.0Europe71.5816310.4434Czech Republic199210315702.0Europe72.414297.02122Czech Republic199710300707.0Europe74.0116048.51424Czech Republic200210256295.0Europe75.5117596.21022Czech Republic200710228744.0Europe76.48622833.30851Denmark19524334000.0Europe70.789692.385245Denmark19574487831.0Europe71.8111099.65935

Custom Font Colors

In [7]:
import plotly.figure_factory as ff

text = [['Team', 'Rank'], ['A', 1], ['B', 2], ['C', 3], ['D', 4], ['E', 5], ['F', 6]]

colorscale = [[0, '#272D31'],[.5, '#ffffff'],[1, '#ffffff']]
font=['#FCFCFC', '#00EE00', '#008B00', '#004F00', '#660000', '#CD0000', '#FF3030']

fig = ff.create_table(text, colorscale=colorscale, font_colors=font)
fig.layout.width=250
fig.show()
TeamRankA1B2C3D4E5F6

Change Font Size

In [8]:
import plotly.figure_factory as ff

data_matrix = [['Country', 'Year', 'Population'],
               ['United States', 2000, 282200000],
               ['Canada', 2000, 27790000],
               ['United States', 2005, 295500000],
               ['Canada', 2005, 32310000],
               ['United States', 2010, 309000000],
               ['Canada', 2010, 34000000]]

fig = ff.create_table(data_matrix, index=True)

# Make text size larger
for i in range(len(fig.layout.annotations)):
    fig.layout.annotations[i].font.size = 20

fig.show()
CountryYearPopulationUnited States2000282200000Canada200027790000United States2005295500000Canada200532310000United States2010309000000Canada201034000000

Tables with Graphs

In [9]:
import plotly.graph_objs as go
import plotly.figure_factory as ff

# Add table data
table_data = [['Team', 'Wins', 'Losses', 'Ties'],
              ['Montréal<br>Canadiens', 18, 4, 0],
              ['Dallas Stars', 18, 5, 0],
              ['NY Rangers', 16, 5, 0],
              ['Boston<br>Bruins', 13, 8, 0],
              ['Chicago<br>Blackhawks', 13, 8, 0],
              ['LA Kings', 13, 8, 0],
              ['Ottawa<br>Senators', 12, 5, 0]]
# Initialize a figure with ff.create_table(table_data)
fig = ff.create_table(table_data, height_constant=60)

# Add graph data
teams = ['Montréal Canadiens', 'Dallas Stars', 'NY Rangers',
         'Boston Bruins', 'Chicago Blackhawks', 'LA Kings', 'Ottawa Senators']
GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 2.45, 3.18]
GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.14, 2.77]
# Make traces for graph
fig.add_trace(go.Scatter(x=teams, y=GFPG,
                    marker=dict(color='#0099ff'),
                    name='Goals For<br>Per Game',
                    xaxis='x2', yaxis='y2'))
fig.add_trace(go.Scatter(x=teams, y=GAPG,
                    marker=dict(color='#404040'),
                    name='Goals Against<br>Per Game',
                    xaxis='x2', yaxis='y2'))

fig.update_layout(
    title_text = '2016 Hockey Stats',
    margin = {'t':50, 'b':100},
    xaxis = {'domain': [0, .5]},
    xaxis2 = {'domain': [0.6, 1.]},
    yaxis2 = {'anchor': 'x2', 'title': 'Goals'}
)

fig.show()
Montréal CanadiensDallas StarsNY RangersBoston BruinsChicago BlackhawksLA KingsOttawa Senators22.22.42.62.833.23.43.6
Goals ForPer GameGoals AgainstPer Game2016 Hockey StatsGoalsTeamWinsLossesTiesMontréalCanadiens1840Dallas Stars1850NY Rangers1650BostonBruins1380ChicagoBlackhawks1380LA Kings1380OttawaSenators1250
In [10]:
import plotly.graph_objs as go
import plotly.figure_factory as ff

# Add table data
table_data = [['Team', 'Wins', 'Losses', 'Ties'],
              ['Montréal<br>Canadiens', 18, 4, 0],
              ['Dallas Stars', 18, 5, 0],
              ['NY Rangers', 16, 5, 0],
              ['Boston<br>Bruins', 13, 8, 0],
              ['Chicago<br>Blackhawks', 13, 8, 0],
              ['Ottawa<br>Senators', 12, 5, 0]]
# Initialize a fig with ff.create_table(table_data)
fig = ff.create_table(table_data, height_constant=60)

# Add graph data
teams = ['Montréal Canadiens', 'Dallas Stars', 'NY Rangers',
         'Boston Bruins', 'Chicago Blackhawks', 'Ottawa Senators']
GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 3.18]
GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.77]

fig.add_trace(go.Bar(x=teams, y=GFPG, xaxis='x2', yaxis='y2',
                marker=dict(color='#0099ff'),
                name='Goals For<br>Per Game'))

fig.add_trace(go.Bar(x=teams, y=GAPG, xaxis='x2', yaxis='y2',
                marker=dict(color='#404040'),
                name='Goals Against<br>Per Game'))

fig.update_layout(
    title_text = '2016 Hockey Stats',
    height = 800,
    margin = {'t':75, 'l':50},
    yaxis = {'domain': [0, .45]},
    xaxis2 = {'anchor': 'y2'},
    yaxis2 = {'domain': [.6, 1], 'anchor': 'x2', 'title': 'Goals'}
)

fig.show()
Montréal CanadiensDallas StarsNY RangersBoston BruinsChicago BlackhawksOttawa Senators00.511.522.533.5
Goals ForPer GameGoals AgainstPer Game2016 Hockey StatsGoalsTeamWinsLossesTiesMontréalCanadiens1840Dallas Stars1850NY Rangers1650BostonBruins1380ChicagoBlackhawks1380OttawaSenators1250

Reference

For more info on ff.create_table(), see the full function reference

What About Dash?

Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash at https://dash.plot.ly/installation.

Everywhere in this page that you see fig.show(), you can display the same figure in a Dash application by passing it to the figure argument of the Graph component from the built-in dash_core_components package like this:

import plotly.graph_objects as go # or plotly.express as px
fig = go.Figure() # or any Plotly Express function e.g. px.bar(...)
# fig.add_trace( ... )
# fig.update_layout( ... )

from dash import Dash, dcc, html

app = Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run_server(debug=True, use_reloader=False)  # Turn off reloader if inside Jupyter