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
Selected Reading
How to show the title for the diagram of Seaborn pairplot() or PridGrid()? (Matplotlib)
To show the title for the diagram for Seaborn pairplot() or PairGrid(), we can use the fig.suptitle() method. This adds a centered title above all subplots in the figure.
Steps
- Set the figure size and adjust the padding between and around the subplots
- Create a Pandas DataFrame with sample data
- Plot pairwise relationships using
pairplot()orPairGrid() - Add a centered title to the figure using
fig.suptitle() - Display the figure using
show()method
Example with pairplot()
Here's how to add a title to a seaborn pairplot ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame(np.random.random((5, 5)),
columns=["a", "b", "c", "d", "e"])
pp = sns.pairplot(df, height=2.0)
pp.fig.suptitle("My Pairplot Title")
plt.show()
Example with PairGrid()
You can also add titles to PairGrid objects in the same way ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
df = pd.DataFrame({
'x': np.random.normal(0, 1, 100),
'y': np.random.normal(0, 1, 100),
'z': np.random.normal(0, 1, 100)
})
# Create PairGrid
g = sns.PairGrid(df)
g.map_diag(sns.histplot)
g.map_upper(sns.scatterplot)
g.map_lower(sns.scatterplot)
# Add title
g.fig.suptitle("Custom PairGrid with Title")
plt.show()
Adjusting Title Position
You can customize the title position and appearance using additional parameters ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.random((5, 4)),
columns=["Feature A", "Feature B", "Feature C", "Feature D"])
pp = sns.pairplot(df, height=2.0)
# Add title with custom positioning
pp.fig.suptitle("Data Relationships Analysis",
fontsize=16,
y=1.02) # y=1.02 moves title slightly up
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
fontsize |
Size of the title text | fontsize=16 |
y |
Vertical position (0-1) | y=1.02 |
fontweight |
Weight of the font | fontweight='bold' |
Conclusion
Use fig.suptitle() on pairplot or PairGrid objects to add titles. Adjust positioning with the y parameter and use plt.tight_layout() to prevent overlap with subplots.
Advertisements
