12-pairplot
August 13, 2024
1 Seaborn: pairplot
[1]: import seaborn as sns
from matplotlib import pyplot as plt
import numpy as np
[2]: tips = sns.load_dataset('tips')
[3]: [Link]()
[3]: total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
1.1 Intro Visuals
[4]: [Link]('xtick', labelsize=12)
[Link]('ytick', labelsize=12)
[Link]('axes', labelsize=14)
[5]: sns.set_style('darkgrid')
[6]: [Link](tips, plot_kws={'color':'black'})
plt.tight_layout();
1
[7]: [Link](tips, kind='reg', plot_kws={'line_kws': {'color':'black', 'lw':␣
↪5}})
plt.tight_layout();
2
[8]: [Link](tips, kind='reg',
diag_kws={'color': 'black'}
)
plt.tight_layout();
3
[9]: [Link](tips, diag_kind='kde', kind='reg',
diag_kws={'color': 'black', 'shade':False, 'lw': 4}
)
plt.tight_layout();
4
[10]: [Link]('xtick', labelsize=10)
[Link]('ytick', labelsize=10)
[Link]('axes', labelsize=10)
1.2 Basics
[11]: sns.set_style('darkgrid')
[12]: [Link](tips);
5
[13]: [Link]
[13]: total_bill float64
tip float64
sex category
smoker category
day category
time category
size int64
dtype: object
Note that the axes tick labels correspond to the bivariate, relational plots and NOT the distribution
plots.
6
[14]: [Link](tips['size'],
kde=False,
hist_kws={'alpha':1});
1.3 Plot Kinds
1.3.1 diag_kind
[15]: [Link](tips, diag_kind='kde');
7
1.3.2 kind
[16]: [Link](tips, kind='reg');
8
1.3.3 Returns PairGrid
[17]: g = [Link](tips)
9
[18]: type(g)
[18]: [Link]
[19]: g = [Link](tips)
g.map_upper([Link], n_levels=6, color='xkcd:wine red');
10
1.4 Categorical and Specific Variables
[20]: tips['weekend'] = [Link](['Sat', 'Sun'])
[21]: [Link]()
[21]: total_bill tip sex smoker day time size weekend
0 16.99 1.01 Female No Sun Dinner 2 True
1 10.34 1.66 Male No Sun Dinner 3 True
2 21.01 3.50 Male No Sun Dinner 3 True
3 23.68 3.31 Male No Sun Dinner 2 True
4 24.59 3.61 Female No Sun Dinner 4 True
11
1.4.1 hue
[22]: [Link](tips, hue='weekend');
/usr/local/lib/python3.7/site-packages/statsmodels/nonparametric/[Link]:
RuntimeWarning: invalid value encountered in true_divide
binned = fast_linbin(X, a, b, gridsize) / (delta * nobs)
/usr/local/lib/python3.7/site-packages/statsmodels/nonparametric/[Link]:
RuntimeWarning: invalid value encountered in double_scalars
FAC1 = 2*([Link]*bw/RANGE)**2
[23]: [Link]
[23]: total_bill float64
tip float64
sex category
12
smoker category
day category
time category
size int64
weekend bool
dtype: object
1.4.2 vars, x_vars, y_vars
[24]: [Link](tips, hue='weekend', vars=['total_bill', 'tip']);
[25]: [Link](tips,
hue='weekend',
y_vars=['tip'],
x_vars=['total_bill', 'size'],
kind='reg',
height=4);
13
1.5 Styling
[26]: del tips['weekend']
1.5.1 height, aspect
[27]: from ipywidgets import interactive
def size_widget(height=2.5, aspect=1):
[Link](tips, height=height, aspect=aspect)
[28]: interactive(size_widget, height=(1, 3.5, 0.5), aspect=(0.5, 2, 0.25))
interactive(children=(FloatSlider(value=2.5, description='height', max=3.5,␣
↪min=1.0, step=0.5), FloatSlider(va…
1.5.2 palette
[29]: [Link](tips, hue='day', palette='plasma');
14
1.5.3 diag_kws, plot_kws
[30]: [Link](tips, diag_kws={'color': 'gray', 'alpha': 0.6});
15
[31]: [Link](tips,
kind='reg',
plot_kws={'ci':None, 'color': 'xkcd:salmon',
'scatter_kws': {'color': 'gray', 's': 3}
}
);
16
17