-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
60 lines (43 loc) · 1.52 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial.transform import Rotation as Rot
def rot_mat_2d(angle):
"""
Create 2D rotation matrix from an angle
Parameters
----------
angle :
Returns
-------
A 2D rotation matrix
Examples
--------
>>> angle_mod(-4.0)
"""
return Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
def plot_covariance_ellipse(x, y, cov, chi2=3.0, color="-r", ax=None):
#This function plots an ellipse that represents a covariance matrix. The ellipse is centered at (x, y) and its shape, size and rotation are determined by the covariance matrix.
eig_val, eig_vec = np.linalg.eig(cov)
if eig_val[0] >= eig_val[1]:
big_ind = 0
small_ind = 1
else:
big_ind = 1
small_ind = 0
a = math.sqrt(chi2 * eig_val[big_ind])
b = math.sqrt(chi2 * eig_val[small_ind])
angle = math.atan2(eig_vec[1, big_ind], eig_vec[0, big_ind])
plot_ellipse(x, y, a, b, angle, color=color, ax=ax)
def plot_ellipse(x, y, a, b, angle, color="-r", ax=None, **kwargs):
#This function plots an ellipse based on the given parameters.
t = np.arange(0, 2 * math.pi + 0.1, 0.1)
px = [a * math.cos(it) for it in t]
py = [b * math.sin(it) for it in t]
fx = rot_mat_2d(angle) @ (np.array([px, py]))
px = np.array(fx[0, :] + x).flatten()
py = np.array(fx[1, :] + y).flatten()
if ax is None:
plt.plot(px, py, color, **kwargs)
else:
ax.plot(px, py, color, **kwargs)