def get_error_ellipse(cov, center, nstd, **kwargs):
"""
return error ellipse patch representing the covariance matrix
cov: 2x2 covariance matrix
centre: mean values (mu1, mu2)
nstd: number of standard deviations
"""
# eigenvalues and eigenvectors of the covariance matrix
# (in ascending order of the eigenvalue)
eigvals, eigvecs = np.linalg.eigh(cov)
# x, y component of the eigenvector (eigvecs[:,1]) with the larger eigenvalue
x, y = eigvecs[:,1][0], eigvecs[:,1][1]
# angle of the ellipse
theta = np.arctan2(y, x)
# width = 2 times radius in x, height = 2 times radius in y
height, width = 2 * nstd * np.sqrt(eigvals)
return Ellipse(xy=center, width=width, height=height,
angle=np.degrees(theta), **kwargs)