12.7 C
London
Friday, June 6, 2025
HomeSoftware TutorialsPythonHow to Add a Table to Seaborn Plot (With Example)

How to Add a Table to Seaborn Plot (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The easiest way to add a table to a seaborn plot is to use the table() function from Matplotlib.

The following example shows how to use this function in practice.

Example: How to Add Table to Seaborn Plot

Suppose we have the following pandas DataFrame that contains information about basketball players on various teams:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28, 30],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4, 15]})

#view DataFrame
print(df)

  team  points  assists
0    A      18        5
1    A      22        7
2    A      19        7
3    B      14        9
4    B      14       12
5    B      11        9
6    C      20        9
7    C      28        4
8    C      30       15

The following code shows how to create a scatterplot in seaborn and use the table() function from Matplotlib to add a table below the plot that shows the raw data values:

import seaborn as sns
import matplotlib.pyplot as plt

#create scatterplot of assists vs points
sns.scatterplot(data=df, x='assists', y='points', hue='team')

#add table below scatterplot
table = plt.table(cellText=df.values,
                  rowLabels=df.index,
                  colLabels=df.columns,
                  bbox=(.2, -.7, 0.5, 0.5))

#display final plot
plt.show()

add table to seaborn plot

The table below the plot shows the raw data values represented in the scatterplot.

The bbox argument within the table() function controls the location of the table.

The bbox argument accepts four values to specify the left, top, right, and bottom padding on the table.

We can adjust the values in the bbox argument to instead place the table on the right side of the plot:

import seaborn as sns
import matplotlib.pyplot as plt

#create scatterplot of assists vs points
sns.scatterplot(data=df, x='assists', y='points', hue='team')

#add table to the right of the scatterplot
table = plt.table(cellText=df.values,
                  rowLabels=df.index,
                  colLabels=df.columns,
                  bbox=(1.1, .2, 0.5, 0.5))

#display final plot
plt.show()

seaborn add table to right side of plot

Feel free to play around with the values to get the table in the exact location you’d like.

Note: You can find the complete documentation for the Matplotlib table() function here.

Additional Resources

The following tutorials explain how to perform other common tasks using seaborn:

How to Add a Title to Seaborn Plots
How to Change Font Size in Seaborn Plots
How to Adjust Number of Ticks in Seaborn Plots

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories