forked from Visualize-ML/Book3_Elements-of-Mathematics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBk3_Ch22_1.py
More file actions
55 lines (40 loc) · 1.43 KB
/
Bk3_Ch22_1.py
File metadata and controls
55 lines (40 loc) · 1.43 KB
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
###############
# Authored by Weisheng Jiang
# Book 3 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk3_Ch22_1
import numpy as np
import matplotlib.pyplot as plt
# draw vectors starting from origin
def draw_vector(vector,RBG,label):
array = np.array([[0, 0, vector[0], vector[1]]])
X, Y, U, V = zip(*array)
plt.quiver(X, Y, U, V,angles='xy', scale_units='xy',scale=1,color = RBG)
# add labels to the sample data
label = label + f" ({vector[0]},{vector[1]})"
plt.annotate(label, # text
(vector[0],vector[1]), # point to label
textcoords="offset points",
xytext=(0,10),
# distance from text to points (x,y)
ha='center')
# horizontal alignment center
# define one vector
a = np.array([4,3])
i = np.array([1,0])
j = np.array([0,1])
fig, ax = plt.subplots()
draw_vector(4*i, np.array([0,112,192])/255, '4i')
draw_vector(3*j, np.array([255,0,0])/255, '3j')
draw_vector(i, np.array([0,112,192])/255, 'i')
draw_vector(j, np.array([255,0,0])/255, 'j')
draw_vector(a,np.array([146,208,80])/255, 'a')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.axis('scaled')
ax.set_xlim([0, 5])
ax.set_ylim([0, 5])
ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5])
plt.show()