-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
37 lines (30 loc) · 1.24 KB
/
utils.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
import matplotlib.pyplot as plt
import numpy as np
def plot_lines(M):
x_1 = np.linspace(-10,10,100)
x_2_line_1 = (M[0,2] - M[0,0] * x_1) / M[0,1]
x_2_line_2 = (M[1,2] - M[1,0] * x_1) / M[1,1]
_, ax = plt.subplots(figsize=(10, 10))
ax.plot(x_1, x_2_line_1, '-', linewidth=2, color='#0075ff',
label=f'$x_2={-M[0,0]/M[0,1]:.2f}x_1 + {M[0,2]/M[0,1]:.2f}$')
ax.plot(x_1, x_2_line_2, '-', linewidth=2, color='#ff7300',
label=f'$x_2={-M[1,0]/M[1,1]:.2f}x_1 + {M[1,2]/M[1,1]:.2f}$')
A = M[:, 0:-1]
b = M[:, -1::].flatten()
d = np.linalg.det(A)
if d != 0:
solution = np.linalg.solve(A,b)
ax.plot(solution[0], solution[1], '-o', mfc='none',
markersize=10, markeredgecolor='#ff0000', markeredgewidth=2)
ax.text(solution[0]-0.25, solution[1]+0.75, f'$(${solution[0]:.0f}$,{solution[1]:.0f})$', fontsize=14)
ax.tick_params(axis='x', labelsize=14)
ax.tick_params(axis='y', labelsize=14)
ax.set_xticks(np.arange(-10, 10))
ax.set_yticks(np.arange(-10, 10))
plt.xlabel('$x_1$', size=14)
plt.ylabel('$x_2$', size=14)
plt.legend(loc='upper right', fontsize=14)
plt.axis([-10, 10, -10, 10])
plt.grid()
plt.gca().set_aspect("equal")
plt.show()