-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
31 lines (25 loc) · 950 Bytes
/
plot.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
import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from matplotlib.pyplot import figure
def plot_PCA(y, batch, index = "Site", name = "fig1"):
figure(figsize=(8, 8), dpi=300)
plt.rcParams.update({'font.size': 15})
# numpy
y = np.array(y)
batch = np.array(batch)
# standardize
scaler = StandardScaler()
data_standardized = scaler.fit_transform(y)
# PCA
pca = PCA(n_components=2)
principal_components = pca.fit_transform(data_standardized)
# plot by subject
for b in list(set(batch)):
plt.scatter(principal_components[batch == b, 0], principal_components[batch == b, 1], label = index+" "+str(b))
plt.xlabel("PCA component 1")
plt.ylabel("PCA component 2")
plt.legend(loc = "upper right")
plt.savefig("Figures/{}.png".format(name), dpi = 300, bbox_inches="tight")
plt.close()