Plotly : How to display Table (Figure Factories)

Plotly

 In a previous article, I summarized how to draw a table in Plotly using "graph_objects". (Plotly : How to draw a table (go.Table) However, there is another way to draw a table in Plotly, using "Figure Factories", so we will introduce that too!

What is Figure Factory ?

 In my previous article, I mentioned that Plotly has two main modules, "graph_objects" and "express". (What is Plotly graph_object? (How is it different from Express?))

 So, there is one more module that we didn't talk about, and that is the "Figure Factory".

 It was originally used to plot things that were difficult to create with graph_objects before the creation of Plotly Express, and is being deprecated for things that overlap with Express.

Differences in drawing a table (graph_objects vs figure factory)

 The main features can be summarized as follows. Merit Demerit

graph_objects

  • You can move the table column by column.
  • It takes a lot of work to draw a table.

figure_factory

  • Easy to draw a table!
  • You can't move the table column by column

Minimum requirements for drawing diagrams in Figure Factory

  Figure Factory makes it pretty easy, especially when converting from a pandas dataframe, with the following single line! (Also, the FIgure Factory is often abbreviated as ff)

fig = ff.create_table(df)

 It's so easy to convert, just run the create_table function with the argument being a dataframe!  After that, use fig.show() to display the figure!  Also, the argument can be in the form of a data frame or a matrix! In the next application, I will show you how to use a matrix as an argument.

  In this article, as well as in Plotly : How to draw a table (go.Table) , I actually drew a table using the example of "LifeExp (life expectancy), pop (population), and gdpPercap (GDP per capita) by region and year" and it looks like this!

Sample Code

import plotly.graph_objects as go
import plotly.express as px
import plotly.figure_factory as ff
df = px.data.gapminder()
df= df.groupby(['continent','year']).mean().reset_index().drop('iso_num',axis=1)
df = df.round(4) # To the 4th decimal place.

fig = ff.create_table(df)
fig.show()
Copied title and URL