10 Regplot
10 Regplot
1 Seaborn: regplot
[1]: import seaborn as sns
from matplotlib import pyplot as plt
diamonds.shape
[3]: diamonds.head()
diamonds.shape
[ ]: sns.set_style('white')
plt.rc('xtick', labelsize=14)
plt.rc('ytick', labelsize=14)
1
[ ]: x_vals = np.random.rand(100)*5
y_vals_posTwo = x_vals*2 + np.random.rand(100)*4
y_vals_posHalf = x_vals*0.5 + np.random.rand(100)*3 + 2
y_vals_negOne = x_vals*(-1) + np.random.rand(100)*3 + 6
[ ]: plt.figure(figsize=(3, 5))
sns.regplot(x_vals, y_vals_posTwo, scatter_kws={'alpha': 0.4}, line_kws={'lw':␣
↪4})
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.figure(figsize=(3, 5))
sns.regplot(x_vals, y_vals_posHalf, scatter_kws={'alpha': 0.4}, line_kws={'lw':␣
↪4})
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.figure(figsize=(3, 5))
sns.regplot(x_vals, y_vals_negOne, scatter_kws={'alpha': 0.4}, line_kws={'lw':␣
↪4})
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.figure(figsize=(3, 5))
plt.gca().set(xlim=(0, 5))
sns.regplot(x_vals, y_vals_posTwo, scatter=False, ci=None, line_kws={'lw': 4})
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.figure(figsize=(3, 5))
plt.gca().set(xlim=(0, 5))
sns.regplot(x_vals, y_vals_posHalf, scatter=False, ci=None, line_kws={'lw': 4})
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
2
[ ]: plt.figure(figsize=(3, 5))
plt.gca().set(xlim=(0, 5))
sns.regplot(x_vals, y_vals_negOne, scatter=False, ci=None, line_kws={'lw': 4})
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.figure(figsize=(3, 5))
plt.gca().set(xlim=(0, 5))
sns.regplot(x_vals, y_vals_posHalf, fit_reg=False)
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.figure(figsize=(3, 5))
plt.gca().set(xlim=(0, 5))
sns.regplot(x_vals, y_vals_posTwo, fit_reg=False)
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.figure(figsize=(3, 5))
plt.gca().set(xlim=(0, 5))
sns.regplot(x_vals, y_vals_negOne, fit_reg=False)
sns.despine()
plt.xlim(0, 5)
plt.ylim(0, 12)
plt.tight_layout();
[ ]: plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
1.2 Basics
[5]: sns.set_style('dark')
3
[7]: sns.regplot(x='carat', y='price', data=diamonds);
4
[8]: sns.regplot(x='carat', y='price', data=diamonds, fit_reg=False);
[9]: plt.gca().set(xlim=(0,2.6))
sns.regplot(x='carat', y='price', data=diamonds, scatter=False);
5
1.3 regplot Options
1.3.1 Confidence Intervals
[10]: sns.regplot(x='carat', y='price', data=diamonds,
ci=None
);
diamonds['cut_value'] = diamonds.cut.map(cut_map)
[12]: diamonds.cut_value.value_counts()
6
[12]: 5 80
4 63
3 39
2 11
1 7
Name: cut_value, dtype: int64
Jitter
[13]: sns.regplot(x='cut_value', y='price', data=diamonds,
x_jitter=0.1
);
Estimator Aggregate
[14]: import numpy as np
7
1.3.3 Models
Polynomial Regression (order)
[16]: sns.regplot(x='carat', y='price', data=diamonds,
fit_reg=False
);
8
[17]: sns.regplot(x='carat', y='price', data=diamonds,
order=2
);
9
Robust Regression
[18]: x_example=[0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
y_example=[0.1, 0.8, 2.2, 2.7, 3.8, 4.5, 6.2, 6.8, 7.9, 9.4, 30.4]
[19]: sns.regplot(x=x_example,
y=y_example,
ci=None
);
[20]: sns.regplot(x=x_example,
y=y_example,
ci=None,
robust=True
);
10
1.4 Styling
[21]: sns.set_style('white')
1.4.1 marker
[22]: sns.regplot(x='carat', y='price', data=diamonds,
marker='d'
);
11
1.4.2 scatter_kws
12
1.4.3 line_kws
13
1.5 Related Seaborn Plots
[26]: blue, orange, green, red = sns.color_palette()[:4]
14
[28]: sns.jointplot(x='carat', y='price', data=diamonds,
kind='reg',
color='purple')
plt.xlabel('')
plt.ylabel('')
plt.xlim(-0.1, None)
plt.ylim(-2000, None)
plt.tight_layout();
15
[29]: sns.pairplot(diamonds[['carat', 'depth', 'price']],
kind='reg',
palette='colorblind')
plt.tight_layout();
16
[ ]:
17