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:
Data Preparation: Random values are generated for four features using NumPy and Pandas DataFrame stores the dataset.
Creating Subplots: A 4ร4 grid of subplots is created to display the pairwise relationships. plt.subplots(num_features, num_features, figsize=(10, 10)) sets up the grid layout.
Plotting the Pair Plot: If i == j, a histogram is plotted on the diagonal using ax.hist(). If i โ j, a scatter plot is created using ax.scatter().
Adding Regression Lines: The np.polyfit(x, y, 1) function computes the slope (m) and intercept (b) of the regression line. The ax.plot(x, m*x + b, color="red", linewidth=1) function overlays a red regression line on the scatter plot.
Labels are added to only the leftmost and bottom plots. Ticks are hidden for a clean design.
plt.tight_layout() ensures proper spacing for readability.