Last active
August 9, 2019 17:03
-
-
Save dkirkby/6b040f2ca29b5709f561c18a44a6e204 to your computer and use it in GitHub Desktop.
Draw an ellipse starting from different parameterizations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given ellipticity parameters (e1, e2) and size s = det(Q)**(1/4) | |
# https://github.com/dkirkby/desietcimg/blob/master/desietcimg/plot.py | |
def draw_ellipse_se1e2(ax, x0, y0, s, g1, g2, nsigmas=1, **ellipseopts): | |
g = np.sqrt(g1 ** 2 + g2 ** 2) | |
if g > 1: | |
raise ValueError('g1 ** 2 + g2 ** 2 > 1') | |
center = np.array([x0, y0]) | |
angle = np.rad2deg(0.5 * np.arctan2(g2, g1)) | |
ratio = np.sqrt((1 + g) / (1 - g)) | |
width = 2 * s * ratio * nsigmas | |
height = 2 * s / ratio * nsigmas | |
kwargs = dict(color='r', ls='-', lw=2, alpha=0.7, fill=False) | |
kwargs.update(ellipseopts) | |
ellipse = matplotlib.patches.Ellipse(center, width, height, angle, **kwargs) | |
ax.add_artist(ellipse) | |
# Given covariance parameters | |
# https://github.com/dkirkby/MachineLearningStatistics/blob/master/mls/plot.py | |
def draw_ellipse_cov(ax, center, cov, nsigmas=2, **ellipseopts): | |
U, s, _ = np.linalg.svd(cov) | |
angle = np.degrees(np.arctan2(U[1, 0], U[0, 0])) | |
width, height = 2 * nsigmas * np.sqrt(s.T) | |
kwargs = dict(color='w', ls='-', lw=2, fill=False) | |
kwargs.update(ellipseopts) | |
ellipse = matplotlib.patches.Ellipse(center, width, height, angle, **kwargs) | |
ax.add_artist(ellipse) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment