Table of Contents
Adding a table to a Seaborn plot is a simple and effective way to present data in a visually appealing manner. This can be achieved by using the “table” function in the Seaborn library, which allows for the creation of a table with customizable properties such as font size, color, and alignment. To add a table to a Seaborn plot, first, import the necessary libraries and data. Then, use the “table” function to specify the data to be displayed in the table and its corresponding properties. Finally, use the “axes” function to add the table to the desired location on the plot. As an example, the following code adds a table to a Seaborn scatter plot with information on the average temperature and precipitation for different cities:
import seaborn as sns
import pandas as pd
data = pd.read_csv(‘city_data.csv’)
table_data = data.groupby(‘City’).mean()
sns.scatterplot(x=’Temperature’, y=’Precipitation’, data=data)
table = sns.table(table_data, loc=’top’)
axes = table.axes
axes.set_position([0.2, 0.7, 0.5, 0.2])
plt.show()
This code will result in a scatter plot with a table displaying the average temperature and precipitation for each city at the top of the plot. By following these simple steps, one can easily add a table to a Seaborn plot and enhance the presentation of their data.
Add a Table to Seaborn Plot (With Example)
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 15The 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.pyplotas 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()
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.pyplotas 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()
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 .
Cite this article
stats writer (2024). How can I add a table to a Seaborn plot with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-table-to-a-seaborn-plot-with-an-example/
stats writer. "How can I add a table to a Seaborn plot with an example?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-table-to-a-seaborn-plot-with-an-example/.
stats writer. "How can I add a table to a Seaborn plot with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-table-to-a-seaborn-plot-with-an-example/.
stats writer (2024) 'How can I add a table to a Seaborn plot with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-table-to-a-seaborn-plot-with-an-example/.
[1] stats writer, "How can I add a table to a Seaborn plot with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add a table to a Seaborn plot with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
