# 6.2.6 Tables

There are two methods to create a table in Plotly. One is `plotly.graph_objects,` the other is `plotly.figure_factory`

## Graph Objects

### 1. Basic Table

```
import plotly.graph_objects as go

fig = go.Figure(data=[go.Table(header=dict(values=['Jonas', 'Melo']),
                 cells=dict(values=[[100, 90, 80, 85], [90, 85, 88, 65]]))
                     ])
fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDBkNcHkpNnBeGqfMwf%2F-MDC-iqnahSGn3Z9Wpmh%2FScreenshot%202020-07-26%20at%2022.34.47.png?alt=media\&token=ae72c7b6-b7a7-47ce-8585-02b8a88b25ea)

### 2. Customized Table

```
fig = go.Figure(data=[go.Table(
    header=dict(values=['Jonas', 'Melo'],
                line_color='silver',
                fill_color='navy',
                align='center',
                font=dict(color='white', size=14),
                height=40),
    cells=dict(values=[[100, 90, 80, 85], # 1st column
                       [90, 85, 88, 65]], # 2nd column
               line_color='silver',
               fill_color='aliceblue',
               align='left'))
])

fig.update_layout(width=500, height=300)
fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDBkNcHkpNnBeGqfMwf%2F-MDC16i72WagE8WRcDZg%2FScreenshot%202020-07-26%20at%2022.34.41.png?alt=media\&token=a8ab45a8-7d25-4aad-9261-e1b1e15592dc)

## Figure Factory

### 3. Basic Table

```
import plotly.figure_factory as ff
data_matrix = [['Name', 'Semester', 'Score'],
               ['Jonas', 1, 'A+'],
               ['Melo', 1, 'B+'],
               ['Jonas', 2, 'A'],
               ['Melo', 2, 'A']]

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

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDBkNcHkpNnBeGqfMwf%2F-MDC1gQVKLqRqNCN9kIz%2Fnewplot%20\(3\).png?alt=media\&token=76e2cec8-35fc-4325-98f3-19dc535f2a51)

### 4. Table with LaTeX

{% hint style="info" %}
The default row height is 30 pixels. We can use \ **height\_constant**  to change the height of each row.
{% endhint %}

```
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,height_constant=40)
fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDBkNcHkpNnBeGqfMwf%2F-MDC25fapmGHmYiqWJFm%2Fnewplot%20\(2\).png?alt=media\&token=991b99e1-e0c9-4eac-9255-0b9dcd220858)

### 5. Customized Table

{% hint style="info" %}
**`colorscale` for the table :**

* &#x20;value 0 is the header color
* .5 is the first table color
* 1 is the second table color
  {% endhint %}

```
# load dataset and sample it
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/diabetes.csv')
df_sample = df[100: 110]

colorscale = [[0, '#0080ff'],[.5, 'silver'],[1, '#ffffff']]
fig =  ff.create_table(df_sample, colorscale=colorscale)

fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDBkNcHkpNnBeGqfMwf%2F-MDC3jI17pwZgs_ViFLe%2Fnewplot%20\(3\).png?alt=media\&token=6c9d3c5b-dc64-4096-b8b8-89edd23caccf)

{% hint style="info" %}

* Customize font size
* Customize height of each row.
* Address index in the table
  {% endhint %}

```
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,height_constant=40,index=True)
# Make text size larger
for i in range(len(fig.layout.annotations)):
    fig.layout.annotations[i].font.size = 16

fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDBkNcHkpNnBeGqfMwf%2F-MDC3sXEqNM7xwesNVrg%2Fnewplot%20\(4\).png?alt=media\&token=339d7683-7df1-42c7-a377-990c7e2db3b5)
