plotly

Plotly.graph_objects.Table

Tables are one of the most useful features when working with statistical data. They allow you to organize the data in rows and columns which are easy to view and interpret.

It is therefore no surprise that we can create Tables using Plotly and the graph_objects module as we will show in this article.

Plotly.graph_objects.Table

We can use the Table function from the graph_objects class to create a table in Plotly. The function syntax is as follows:

plotly.graph_objects.Table(arg=None, cells=None, columnorder=None, columnordersrc=None, columnwidth=None, columnwidthsrc=None, customdata=None, customdatasrc=None, domain=None, header=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, ids=None, idssrc=None, legendgrouptitle=None, legendrank=None, meta=None, metasrc=None, name=None, stream=None, uid=None, uirevision=None, visible=None, **kwargs)

The following are some important parameters that you can use to create a Table object:

  1. arg – Defines a dictionary containing a list of properties compatible with the constructor or an instance of the Table class.
  2. cells – Defines the cells instance.
  3. columnorder – Sets the rendered order of the columns.
  4. columnwidth – Sets the width of the columns as a ratio.

Example 1:

The following example shows how we can create a simple table using the Table method from the graph_objects class.

importplotly.graph_objectsas go
fig = go.Figure(
    data=[go.Table(
header=dict(values=['Column_A', 'Column_B']),
        cells=dict(values=[['row_A1', 'row_A2', 'row_A3'], ['row_B1', 'row_B2', 'row_B3']]))])
fig.show()

This returns a Table Figure as shown in the following:

Table Styling

We can also style the table by setting the various parameters as shown in the following:

import plotly.graph_objects as go
fig = go.Figure(
    data=[go.Table(
header=dict(values=['Column_A', 'Column_B'],
line_color='slategray',
fill_color='lightskyblue',
        align='left'),
        cells=dict(values=[['row_A1', 'row_A2', 'row_A3'], ['row_B1', 'row_B2', 'row_B3']],
line_color='slategray',
fill_color='lightcyan',
        align='left'))])
fig.show()

The resulting table is as follows:

Setting the Table Width and Height

We can also set the width and height of the table as:

fig.update_layout(width=600, height=400)

Setting the Row and Column Size

You can also change the size of the rows and columns as a ratio. Take the following code:

importplotly.graph_objectsas go
fig = go.Figure(
    data=[go.Table(
columnwidth=[1,2],
header=dict(values=['Column_A', 'Column_B'],
line_color='slategray',
fill_color='lightskyblue',
        align='left'),
        cells=dict(values=[['row_A1', 'row_A2', 'row_A3'], ['row_B1', 'row_B2', 'row_B3']],
line_color='slategray',
fill_color='lightcyan',
        align='left'))])
fig.show()

The given code creates a table with column ratio of 1 to 2.

Output:

Conclusion

This article explores the basics of working with tables in Plotly using the graph_objects module. Feel free to explore the other documents for more.

Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list