A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/pairplot-in-matplotlib/ below:

Pairplot in Matplotlib - GeeksforGeeks

Pairplot in Matplotlib

Last Updated : 23 Jul, 2025

Pair Plot is a type of chart that shows how different numbers in a dataset relate to each other. It creates multiple small scatter plots, comparing two variables at a time. While Seaborn has a ready-made pairplot() function to quickly create this chart, Matplotlib allows more control to customize how the plot looks and behaves. A Pair Plot (also called a scatterplot matrix) consists of:

This visualization helps in identifying:

Creating a pair plot using matplotlib

To get started, we first need to import the necessary libraries.

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np

Implementation: Python
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

np.random.seed(42)
data = pd.DataFrame({
    'Feature 1': np.random.rand(50),
    'Feature 2': np.random.rand(50),
    'Feature 3': np.random.rand(50),
    'Feature 4': np.random.rand(50)
})

# Number of features
num_features = len(data.columns)

# Create Subplots Grid
fig, axes = plt.subplots(num_features, num_features, figsize=(10, 10))

# Loop through each pair of features
for i in range(num_features):
    for j in range(num_features):
        ax = axes[i, j]
        
        if i == j:
            # Diagonal: Histogram of the feature
            ax.hist(data.iloc[:, i], bins=15, color='skyblue', edgecolor='black')
        else:
            # Scatter plot for feature pairs
            ax.scatter(data.iloc[:, j], data.iloc[:, i], alpha=0.7, s=10, color="blue")

        # Set labels on the left and bottom axes
        if j == 0:
            ax.set_ylabel(data.columns[i], fontsize=10)
        if i == num_features - 1:
            ax.set_xlabel(data.columns[j], fontsize=10)

        # Remove ticks for a cleaner look
        ax.set_xticks([])
        ax.set_yticks([])

# Adjust layout
plt.tight_layout()
plt.show()

Output

Explanation:

Advantages of pair plot in matplotlib Enhancing the pair plot

To improve the visualization, consider:

Example:

Python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

np.random.seed(42)
data = pd.DataFrame(np.random.rand(50, 4), columns=['Feature 1', 'Feature 2', 'Feature 3', 'Feature 4'])

# Number of features
num_features = len(data.columns)

# Create figure
fig, axes = plt.subplots(num_features, num_features, figsize=(10, 10))

# Loop through each pair of features
for i in range(num_features):
    for j in range(num_features):
        ax = axes[i, j]

        if i == j:
            # Plot histogram on the diagonal
            ax.hist(data.iloc[:, i], bins=10, color="skyblue", edgecolor="black")
        else:
            # Scatter plot
            x = data.iloc[:, j]
            y = data.iloc[:, i]
            ax.scatter(x, y, alpha=0.7, s=10, color="blue")

            # Add Regression Line
            m, b = np.polyfit(x, y, 1)  # Linear regression
            ax.plot(x, m*x + b, color="red", linewidth=1)

        # Labels
        if j == 0:
            ax.set_ylabel(data.columns[i], fontsize=10)
        if i == num_features - 1:
            ax.set_xlabel(data.columns[j], fontsize=10)

        # Hide ticks for cleaner look
        ax.set_xticks([])
        ax.set_yticks([])

# Adjust layout
plt.tight_layout()
plt.show()

Output:


Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4