Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to display a Dataframe next to a Plot in Jupyter Notebook?
In Jupyter Notebook, you can display a DataFrame and a plot side by side using matplotlib's subplot functionality. This creates a professional-looking visualization that combines both tabular and graphical data representation.
Steps to Display DataFrame Next to Plot
Set the figure size and adjust the padding between and around the subplots
Create a Pandas DataFrame with sample data
Create a figure with two subplots using
add_subplot()Plot the data in the first subplot using
scatter()methodDisplay the DataFrame as a table in the second subplot using
table()methodTurn off axes for the table subplot for cleaner appearance
Example
Here's how to create a side-by-side display of a scatter plot and DataFrame table ?
import matplotlib.pyplot as plt
import pandas as pd
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample DataFrame
df = pd.DataFrame({
'Straight': [i for i in range(10)],
'Square': [i * i for i in range(10)]
})
# Create figure and subplots
fig = plt.figure()
# First subplot: scatter plot
ax1 = fig.add_subplot(121)
ax1.scatter(x=df.Straight, y=df.Square, color='blue', alpha=0.7)
ax1.set_xlabel('Straight Values')
ax1.set_ylabel('Square Values')
ax1.set_title('Scatter Plot')
# Second subplot: DataFrame table
ax2 = fig.add_subplot(122)
ax2.axis('off') # Turn off axes for cleaner table appearance
# Create table
font_size = 14
bbox = [0, 0, 1, 1]
mpl_table = ax2.table(
cellText=df.values,
rowLabels=df.index,
colLabels=df.columns,
bbox=bbox,
cellLoc='center'
)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)
ax2.set_title('DataFrame Table')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Purpose | Description |
|---|---|---|
add_subplot(121) |
First subplot | 1 row, 2 columns, position 1 |
add_subplot(122) |
Second subplot | 1 row, 2 columns, position 2 |
axis('off') |
Hide axes | Removes x and y axes for table |
bbox=[0,0,1,1] |
Table positioning | Full subplot area coverage |
Alternative Layout Options
You can also create vertical layouts by changing the subplot configuration ?
import matplotlib.pyplot as plt
import pandas as pd
# Vertical layout (2 rows, 1 column)
plt.rcParams["figure.figsize"] = [6, 8]
df = pd.DataFrame({
'X': [1, 2, 3, 4, 5],
'Y': [2, 4, 6, 8, 10]
})
fig = plt.figure()
# Plot on top
ax1 = fig.add_subplot(211) # 2 rows, 1 column, position 1
ax1.plot(df.X, df.Y, marker='o')
ax1.set_title('Line Plot')
# Table on bottom
ax2 = fig.add_subplot(212) # 2 rows, 1 column, position 2
ax2.axis('off')
table = ax2.table(cellText=df.values, colLabels=df.columns, bbox=[0, 0, 1, 1])
table.set_fontsize(12)
ax2.set_title('Data Table')
plt.tight_layout()
plt.show()
Conclusion
Using matplotlib's subplot functionality allows you to create professional visualizations combining plots and DataFrames. Use add_subplot() to create side-by-side layouts and table() to display DataFrame data alongside your plots.
