Skip to content

Instantly share code, notes, and snippets.

@sauravrt
Created March 27, 2022 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sauravrt/b2d14af6453d6b28703b60d2136ec55b to your computer and use it in GitHub Desktop.
Save sauravrt/b2d14af6453d6b28703b60d2136ec55b to your computer and use it in GitHub Desktop.
Plot 2-sigma ellipse
def plot_twosigma(pos, S, ax, color='g'):
"""Plot 2-sigma ellipse
Parameters
----------
pos : tuple of length 2
Center of the 2-sigma ellipse
S : numpy.ndarray of size 2x2
Position covariance
ax : matplotlib.axes
Axes handle where the ellipse will be drawn
color : str, optional
Ellipse color, by default 'g'
Returns
-------
matplotlib.axes
Axes where the ellipse was drawn
"""
x = np.linspace(0, 2*np.pi, 360)
uv = np.array([np.cos(x), np.sin(x)]) # unit vector rotating 360deg
v2sigma = 2*np.linalg.cholesky(S)@uv
ax.plot(v2sigma[0, :] + pos[0],
v2sigma[1, :] + pos[1],
lw=1, color=color, alpha=0.5)
return ax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment