-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplotting_functions.py
151 lines (128 loc) · 4.51 KB
/
plotting_functions.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from statsmodels.stats.outliers_influence import summary_table
import statsmodels.api as sm
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
plt.rc("axes.spines", top=False, right=False)
def cm2inch(*tupl):
inch = 2.54
if isinstance(tupl[0], tuple):
return tuple(i / inch for i in tupl[0])
else:
return tuple(i / inch for i in tupl)
def show_img(path):
img = pltimg.imread(path)
f, a = plt.subplots()
a.imshow(img)
a.spines['top'].set_visible(False)
a.spines['right'].set_visible(False)
a.spines['bottom'].set_visible(False)
a.spines['left'].set_visible(False)
a.set_xticks([])
a.set_yticks([])
f.tight_layout()
plt.show()
def plot_correlation(x, y,
xlabel='',
ylabel='',
title='',
ci=0.95,
alpha=0.5,
size=30,
color='red',
markercolor='black',
marker='o',
xticks=None,
yticks=None,
xticklabels=None,
yticklabels=None,
xlim=None,
ylim=None,
annotate=True,
annotation_pos=(0.1, 0.1),
annotation_halign='left',
fontsize_title=7,
fontsize_axeslabel=7,
fontsize_ticklabels=7,
fontsize_annotation=7,
regression=True,
plot_diagonal=False,
return_correlation=False,
ax=None):
# Defaults
if ax is None:
fig, ax = plt.subplots()
# Axes, ticks, ...
if xticks is not None:
ax.set_xticks(xticks)
if yticks is not None:
ax.set_yticks(yticks)
if xticklabels is not None:
ax.set_xticklabels(xticklabels, fontsize=fontsize_ticklabels)
if yticklabels is not None:
ax.set_yticklabels(yticklabels, fontsize=fontsize_ticklabels)
ax.tick_params(axis='both', which='major', labelsize=fontsize_ticklabels)
if xlim is not None:
ax.set_xlim(xlim)
if ylim is not None:
ax.set_ylim(ylim)
# Scatter (translucent dots with solid outlines)
ax.scatter(x, y,
marker='o',
color='none',
edgecolor=markercolor,
linewidth=0.5,
s=size)
ax.scatter(x, y,
marker='o',
color=markercolor,
alpha=alpha,
linewidth=0,
s=size)
if regression:
# LM fit
X = sm.add_constant(x)
lm = sm.OLS(y, X).fit()
intercept, slope = lm.params
table, data, columns = summary_table(lm, alpha=1. - ci)
predicted, mean_ci_lower, mean_ci_upper = data[:, np.array([
2, 4, 5])].T
xs = np.linspace(*ax.get_xlim(), 100)
line = ax.plot(xs, intercept + slope * xs,
color=color)
sort_idx = np.argsort(x)
ax.fill_between(x[sort_idx], mean_ci_lower[sort_idx], mean_ci_upper[sort_idx],
color=color, alpha=0.1)
# Annotation
tval = lm.tvalues[-1]
pval = lm.pvalues[-1]
if pval < 0.0001:
p_string = r'$P < 0.0001$'
else:
p_string = r'$P = {}$'.format(np.round(pval, 4))
r = np.sign(tval) * np.sqrt(lm.rsquared)
annotation = (r'$r = {:.2f}$, '.format(r)) + p_string
if annotate:
ax.text(*annotation_pos,
annotation,
verticalalignment='bottom',
horizontalalignment=annotation_halign,
transform=ax.transAxes,
fontsize=fontsize_annotation)
# Diagonal
if plot_diagonal:
ax.plot([0, 1], [0, 1], transform=ax.transAxes,
color='black', alpha=0.5, zorder=-10, lw=1)
# Labels
ax.set_xlabel(xlabel, fontsize=fontsize_axeslabel)
ax.set_ylabel(ylabel, fontsize=fontsize_axeslabel)
ax.set_title(title, fontsize=fontsize_title)
if return_correlation:
return ax, line, annotation
else:
return ax
def add_regression_line(ax, intercept, slope, color='darkgray', **kwargs):
xs = np.linspace(*ax.get_xlim(), 100)
ax.plot(xs, intercept + slope * xs, color=color, **kwargs)
return ax