Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132923 views
License: OTHER
1
import matplotlib.pyplot as plt
2
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
3
4
5
def plot_decomposition(people, pca):
6
image_shape = people.images[0].shape
7
plt.figure(figsize=(20, 3))
8
ax = plt.gca()
9
10
imagebox = OffsetImage(people.images[0], zoom=1.5, cmap="gray")
11
ab = AnnotationBbox(imagebox, (.05, 0.4), pad=0.0, xycoords='data')
12
ax.add_artist(ab)
13
14
for i in range(4):
15
imagebox = OffsetImage(pca.components_[i].reshape(image_shape), zoom=1.5, cmap="viridis")
16
17
ab = AnnotationBbox(imagebox, (.3 + .2 * i, 0.4),
18
pad=0.0,
19
xycoords='data'
20
)
21
ax.add_artist(ab)
22
if i == 0:
23
plt.text(.18, .25, 'x_%d *' % i, fontdict={'fontsize': 50})
24
else:
25
plt.text(.15 + .2 * i, .25, '+ x_%d *' % i, fontdict={'fontsize': 50})
26
27
plt.text(.95, .25, '+ ...', fontdict={'fontsize': 50})
28
29
plt.rc('text', usetex=True)
30
plt.text(.13, .3, r'\approx', fontdict={'fontsize': 50})
31
plt.axis("off")
32
33