CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
weijie-chen

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: weijie-chen/Linear-Algebra-With-Python
Path: blob/master/notebooks/linear_algebra_visulization.py
Views: 449
1
import matplotlib.pyplot as plt
2
import numpy as np
3
import numpy
4
def linearCombo(a, b, c):
5
'''This function is for visualizing linear combination of standard basis in 3D.
6
Function syntax: linearCombo(a, b, c), where a, b, c are the scalar multiplier,
7
also the elements of the vector.
8
'''
9
fig = plt.figure(figsize = (10,10))
10
ax = fig.add_subplot(projection='3d')
11
12
######################## Standard basis and Scalar Multiplid Vectors#########################
13
vec = np.array([[[0, 0, 0, 1, 0, 0]], # e1
14
[[0, 0, 0, 0, 1, 0]], # e2
15
[[0, 0, 0, 0, 0, 1]], # e3
16
[[0, 0, 0, a, 0, 0]], # a* e1
17
[[0, 0, 0, 0, b, 0]], # b* e2
18
[[0, 0, 0, 0, 0, c]], # c* e3
19
[[0, 0, 0, a, b, c]]]) # ae1 + be2 + ce3
20
colors = ['b','b','b','r','r','r','g']
21
for i in range(vec.shape[0]):
22
X, Y, Z, U, V, W = zip(*vec[i,:,:])
23
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False,
24
color = colors[i] ,arrow_length_ratio = .08, pivot = 'tail',
25
linestyles = 'solid',linewidths = 3, alpha =.6)
26
27
#################################Plot Rectangle Boxes##############################
28
dlines = np.array([[[a, 0, 0],[a, b, 0]],
29
[[0, b, 0],[a, b, 0]],
30
[[0, 0, c],[a, b, c]],
31
[[0, 0, c],[a, 0, c]],
32
[[a, 0, c],[a, b, c]],
33
[[0, 0, c],[0, b, c]],
34
[[0, b, c],[a, b, c]],
35
[[a, 0, 0],[a, 0, c]],
36
[[0, b, 0],[0, b, c]],
37
[[a, b, 0],[a, b, c]]])
38
colors = ['k','k','g','k','k','k','k','k','k']
39
for i in range(dlines.shape[0]):
40
ax.plot(dlines[i,:,0], dlines[i,:,1], dlines[i,:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
41
42
#################################Annotation########################################
43
ax.text(x = a, y = b, z = c, s= ' $(%0.d, %0.d, %.0d)$'% (a, b, c), size = 18)
44
ax.text(x = a, y = 0, z = 0, s= ' $%0.d e_1 = (%0.d, 0, 0)$'% (a, a), size = 15)
45
ax.text(x = 0, y = b, z = 0, s= ' $%0.d e_2 = (0, %0.d, 0)$'% (b, b), size = 15)
46
ax.text(x = 0, y = 0, z = c, s= ' $%0.d e_3 = (0, 0, %0.d)$' %(c, c), size = 15)
47
48
#################################Axis Setting######################################
49
ax.grid()
50
ax.set_xlim([0, a+1])
51
ax.set_ylim([0, b+1])
52
ax.set_zlim([0, c+1])
53
54
ax.set_xlabel('x-axis', size = 18)
55
ax.set_ylabel('y-axis', size = 18)
56
ax.set_zlabel('z-axis', size = 18)
57
58
ax.set_title('Vector $(%0.d, %0.d, %.0d)$ Visualization' %(a, b, c), size = 20)
59
60
ax.view_init(elev=20., azim=15)
61
62
if __name__ == '__main__':
63
a = 7
64
b = 4
65
c = 9
66
linearCombo(a, b, c)
67
68
def linearComboNonStd(a, b, c, vec1, vec2, vec3):
69
'''This function is for visualizing linear combination of non-standard basis in 3D.
70
Function syntax: linearCombo(a, b, c, vec1, vec2, vec3), where a, b, c are the scalar multiplier,
71
ve1, vec2 and vec3 are the basis.
72
'''
73
fig = plt.figure(figsize = (10,10))
74
ax = fig.add_subplot(projection='3d')
75
########################Plot basis##############################
76
vec1 = np.array([[0, 0, 0, vec1[0], vec1[1], vec1[2]]])
77
X, Y, Z, U, V, W = zip(*vec1)
78
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'blue',arrow_length_ratio = .08, pivot = 'tail',
79
linestyles = 'solid',linewidths = 3)
80
81
vec2 = np.array([[0, 0, 0, vec2[0], vec2[1], vec2[2]]])
82
X, Y, Z, U, V, W = zip(*vec2)
83
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'blue',arrow_length_ratio = .08, pivot = 'tail',
84
linestyles = 'solid',linewidths = 3)
85
86
vec3 = np.array([[0, 0, 0, vec3[0], vec3[1], vec3[2]]])
87
X, Y, Z, U, V, W = zip(*vec3)
88
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'blue',arrow_length_ratio = .08, pivot = 'tail',
89
linestyles = 'solid',linewidths = 3)
90
91
###########################Plot Scalar Muliplied Vectors####################
92
avec1 = a * vec1
93
X, Y, Z, U, V, W = zip(*avec1)
94
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'red', alpha = .6,arrow_length_ratio = a/100, pivot = 'tail',
95
linestyles = 'solid',linewidths = 3)
96
97
bvec2 = b * vec2
98
X, Y, Z, U, V, W = zip(*bvec2)
99
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'red', alpha = .6,arrow_length_ratio = b/100, pivot = 'tail',
100
linestyles = 'solid',linewidths = 3)
101
102
cvec3 = c * vec3
103
X, Y, Z, U, V, W = zip(*cvec3)
104
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'red', alpha = .6,arrow_length_ratio = c/100, pivot = 'tail',
105
linestyles = 'solid',linewidths = 3)
106
107
combo = avec1 + bvec2 + cvec3
108
X, Y, Z, U, V, W = zip(*combo)
109
ax.quiver(X, Y, Z, U, V, W, length=1, normalize=False, color = 'green', alpha = .7,arrow_length_ratio = np.linalg.norm(combo)/300, pivot = 'tail',
110
linestyles = 'solid',linewidths = 3)
111
112
#################################Plot Rectangle Boxes##############################
113
point1 = [avec1[0, 3], avec1[0, 4], avec1[0, 5]]
114
point2 = [avec1[0, 3]+bvec2[0, 3], avec1[0, 4]+bvec2[0, 4], avec1[0, 5]+bvec2[0, 5]]
115
line1 = np.array([point1, point2])
116
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
117
118
point1 = [bvec2[0, 3], bvec2[0, 4], bvec2[0, 5]]
119
point2 = [avec1[0, 3]+bvec2[0, 3], avec1[0, 4]+bvec2[0, 4], avec1[0, 5]+bvec2[0, 5]]
120
line1 = np.array([point1, point2])
121
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
122
123
point1 = [bvec2[0, 3], bvec2[0, 4], bvec2[0, 5]]
124
point2 = [cvec3[0, 3]+bvec2[0, 3], cvec3[0, 4]+bvec2[0, 4], cvec3[0, 5]+bvec2[0, 5]]
125
line1 = np.array([point1, point2])
126
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
127
128
point1 = [cvec3[0, 3], cvec3[0, 4], cvec3[0, 5]]
129
point2 = [cvec3[0, 3]+bvec2[0, 3], cvec3[0, 4]+bvec2[0, 4], cvec3[0, 5]+bvec2[0, 5]]
130
line1 = np.array([point1, point2])
131
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
132
133
point1 = [cvec3[0, 3], cvec3[0, 4], cvec3[0, 5]]
134
point2 = [cvec3[0, 3]+avec1[0, 3], cvec3[0, 4]+avec1[0, 4], cvec3[0, 5]+avec1[0, 5]]
135
line1 = np.array([point1, point2])
136
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
137
138
point1 = [avec1[0, 3], avec1[0, 4], avec1[0, 5]]
139
point2 = [cvec3[0, 3]+avec1[0, 3], cvec3[0, 4]+avec1[0, 4], cvec3[0, 5]+avec1[0, 5]]
140
line1 = np.array([point1, point2])
141
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
142
143
##
144
point1 = [avec1[0, 3]+bvec2[0, 3]+cvec3[0, 3],
145
avec1[0, 4]+bvec2[0, 4]+cvec3[0, 4],
146
avec1[0, 5]+bvec2[0, 5]+cvec3[0, 5]]
147
point2 = [cvec3[0, 3]+avec1[0, 3],
148
cvec3[0, 4]+avec1[0, 4],
149
cvec3[0, 5]+avec1[0, 5]]
150
line1 = np.array([point1, point2])
151
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
152
153
##
154
point1 = [avec1[0, 3]+bvec2[0, 3]+cvec3[0, 3],
155
avec1[0, 4]+bvec2[0, 4]+cvec3[0, 4],
156
avec1[0, 5]+bvec2[0, 5]+cvec3[0, 5]]
157
point2 = [cvec3[0, 3]+bvec2[0, 3],
158
cvec3[0, 4]+bvec2[0, 4],
159
cvec3[0, 5]+bvec2[0, 5]]
160
line1 = np.array([point1, point2])
161
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
162
163
##
164
point1 = [avec1[0, 3]+bvec2[0, 3]+cvec3[0, 3],
165
avec1[0, 4]+bvec2[0, 4]+cvec3[0, 4],
166
avec1[0, 5]+bvec2[0, 5]+cvec3[0, 5]]
167
point2 = [bvec2[0, 3]+avec1[0, 3],
168
bvec2[0, 4]+avec1[0, 4],
169
bvec2[0, 5]+avec1[0, 5]]
170
line1 = np.array([point1, point2])
171
ax.plot(line1[:,0], line1[:,1], line1[:,2], lw =3, ls = '--', color = 'black', alpha=0.5)
172
#################################Annotation########################################
173
ax.text(x = vec1[0,3], y = vec1[0,4], z = vec1[0,5], s= ' $v_1 =(%0.d, %0.d, %.0d)$'% (vec1[0,3], vec1[0,4], vec1[0,4]), size = 8)
174
ax.text(x = vec2[0,3], y = vec2[0,4], z = vec2[0,5], s= ' $v_2 =(%0.d, %0.d, %.0d)$'% (vec2[0,3], vec2[0,4], vec2[0,4]), size = 8)
175
ax.text(x = vec3[0,3], y = vec3[0,4], z = vec3[0,5], s= ' $v_3= (%0.d, %0.d, %.0d)$'% (vec3[0,3], vec3[0,4], vec3[0,4]), size = 8)
176
177
ax.text(x = avec1[0,3], y = avec1[0,4], z = avec1[0,5], s= ' $%.0d v_1 =(%0.d, %0.d, %.0d)$'% (a, avec1[0,3], avec1[0,4], avec1[0,4]), size = 8)
178
ax.text(x = bvec2[0,3], y = bvec2[0,4], z = bvec2[0,5], s= ' $%.0d v_2 =(%0.d, %0.d, %.0d)$'% (b, bvec2[0,3], bvec2[0,4], bvec2[0,4]), size = 8)
179
ax.text(x = cvec3[0,3], y = cvec3[0,4], z = cvec3[0,5], s= ' $%.0d v_3= (%0.d, %0.d, %.0d)$'% (c, cvec3[0,3], cvec3[0,4], cvec3[0,4]), size = 8)
180
# ax.text(x = 0, y = b, z = 0, s= ' $%0.d e_2 = (0, %0.d, 0)$'% (b, b), size = 15)
181
# ax.text(x = 0, y = 0, z = c, s= ' $%0.d e_3 = (0, 0, %0.d)$' %(c, c), size = 15)
182
183
#################################Axis Setting######################################
184
ax.grid()
185
ax.set_xlim([0, 15])
186
ax.set_ylim([0, 15])
187
ax.set_zlim([0, 15])
188
189
ax.set_xlabel('x-axis', size = 18)
190
ax.set_ylabel('y-axis', size = 18)
191
ax.set_zlabel('z-axis', size = 18)
192
193
#ax.set_title('Vector $(%0.d, %0.d, %.0d)$ Visualization' %(a, b, c), size = 20)
194
195
ax.view_init(elev=20., azim=15)
196
197
if __name__ == '__main__':
198
a = 2
199
b = 3
200
c = 4
201
vec1 = np.array([2,1,0])
202
vec2 = np.array([0,3,1])
203
vec3 = np.array([1,2,3])
204
linearComboNonStd(a, b, c, vec1,vec2,vec3)
205
206
207