Python / Data Science Essentials Interview Questions
How do Seaborn jointplot and pairplot help explore multivariate relationships?
When you have more than one numeric variable, the next step after individual histograms is to understand relationships between pairs. Seaborn's jointplot and pairplot automate this exploration with minimal code.
import seaborn as sns import matplotlib.pyplot as plt penguins = sns.load_dataset('penguins').dropna() # --- jointplot: one pair of variables --- # Scatter + marginal histograms sns.jointplot(data=penguins, x='bill_length_mm', y='bill_depth_mm', hue='species', height=6) # Regression + 95% confidence interval sns.jointplot(data=penguins, x='flipper_length_mm', y='body_mass_g', kind='reg', height=6) # Hex bins â better than scatter for large datasets with overplotting sns.jointplot(data=penguins, x='flipper_length_mm', y='body_mass_g', kind='hex', height=6) # KDE â smooth 2-D density sns.jointplot(data=penguins, x='bill_length_mm', y='bill_depth_mm', kind='kde', fill=True, height=6) # --- pairplot: all pairs + diagonal histograms --- # Standard scatter matrix sns.pairplot(penguins, hue='species', diag_kind='kde', # diagonal: KDE instead of histogram plot_kws={'alpha': 0.5}, # semi-transparent points height=2.5) plt.suptitle('Penguin Feature Pairs', y=1.02) plt.show() # Subset of columns only cols = ['bill_length_mm', 'flipper_length_mm', 'body_mass_g'] sns.pairplot(penguins[cols + ['species']], hue='species')
Use jointplot when you want to focus deeply on one specific pair of variables with marginal distributions visible. Use pairplot for a broad overview of all pairwise relationships in a dataset with up to ~10 variables — beyond that the grid becomes too small to read meaningfully.
More Related questions...