-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
viz.py
78 lines (61 loc) · 2.11 KB
/
viz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Visualizations for illustrating seaborn, reference lines, and itertools."""
import itertools
import matplotlib.pyplot as plt
import seaborn as sns
def reg_resid_plots(data):
"""
Using `seaborn`, plot the regression and residuals plots
side-by-side for every permutation of 2 columns in the data.
Parameters:
- data: A `pandas.DataFrame`
Returns:
A matplotlib `Axes` object.
"""
num_cols = data.shape[1]
permutation_count = num_cols * (num_cols - 1)
fig, ax = plt.subplots(permutation_count, 2, figsize=(15, 8))
for (x, y), axes, color in zip(
itertools.permutations(data.columns, 2),
ax,
itertools.cycle(['royalblue', 'darkorange'])
):
for subplot, func in zip(axes, (sns.regplot, sns.residplot)):
func(x=x, y=y, data=data, ax=subplot, color=color)
if func == sns.residplot:
# mark the residuals as such
subplot.set_ylabel('residuals')
return fig.axes
def std_from_mean_kde(data):
"""
Plot the KDE along with vertical reference lines
for each standard deviation from the mean.
Parameters:
- data: `pandas.Series` with numeric data
Returns:
Matplotlib `Axes` object.
"""
mean_mag, std_mean = data.mean(), data.std()
ax = data.plot(kind='kde')
ax.axvline(mean_mag, color='b', alpha=0.2, label='mean')
colors = ['green', 'orange', 'red']
multipliers = [1, 2, 3]
signs = ['-', '+']
linestyles = [':', '-.', '--']
for sign, (color, multiplier, style) in itertools.product(
signs, zip(colors, multipliers, linestyles)
):
adjustment = multiplier * std_mean
if sign == '-':
value = mean_mag - adjustment
label = '{} {}{}{}'.format(
r'$\mu$',
r'$\pm$',
multiplier,
r'$\sigma$'
)
else:
value = mean_mag + adjustment
label = None
ax.axvline(value, color=color, linestyle=style, label=label, alpha=0.5)
ax.legend()
return ax