Plot an ellipse
Submitted by:
kevindunn, 22 August 2011
Update history:
Revision 2 of 2:
previous
Updated by:
woiski, 03 September 2011
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 | # Source: scipy-central.org/item/23/1/plot-an-ellipse
# License: Creative Commons Zero (almost public domain) http://scpyce.org/cc0
import numpy as np
from pylab import plot, show, grid
def get_ellipse_coords(a=0.0, b=0.0, x=0.0, y=0.0, angle=0.0, k=2):
""" Draws an ellipse using (360*k + 1) discrete points; based on pseudo code
given at http://en.wikipedia.org/wiki/Ellipse
k = 1 means 361 points (degree by degree)
a = major axis distance,
b = minor axis distance,
x = offset along the x-axis
y = offset along the y-axis
angle = clockwise rotation [in degrees] of the ellipse;
* angle=0 : the ellipse is aligned with the positive x-axis
* angle=30 : rotated 30 degrees clockwise from positive x-axis
"""
pts = np.zeros((360*k+1, 2))
beta = -angle * np.pi/180.0
sin_beta = np.sin(beta)
cos_beta = np.cos(beta)
alpha = np.radians(np.r_[0.:360.:1j*(360*k+1)])
sin_alpha = np.sin(alpha)
cos_alpha = np.cos(alpha)
pts[:, 0] = x + (a * cos_alpha * cos_beta - b * sin_alpha * sin_beta)
pts[:, 1] = y + (a * cos_alpha * sin_beta + b * sin_alpha * cos_beta)
return pts
if __name__ == '__main__':
# Plot a unit circle centered at (+2, +3)
pts = get_ellipse_coords(a=1.0, b=1.0, x=2, y=3,k=1./8)
ax = plot(pts[:,0], pts[:,1])
# Set the aspect ratio so it looks like a circle; add a grid as well
ax[0].get_axes().set_aspect(1)
grid('on')
# Ellipse, with major axis length = 4, minor axis = 1, centered at (0,0)
pts = get_ellipse_coords(a=4.0, b=1.0)
ax = plot(pts[:,0], pts[:,1])
# Rotate the above ellipse by 30 degrees and use only 11 points!
pts = get_ellipse_coords(a=4.0, b=1.0, angle=30,k=1./36)
ax = plot(pts[:,0], pts[:,1])
# Use all the options and 721 points:
pts = get_ellipse_coords(a=2.0, b=0.25, x=-4, y=-2, angle=250,k=2)
ax = plot(pts[:,0], pts[:,1])
show()
|
Software license:
Creative Commons Zero. No rights reserved.
Users have permission to do anything with the code and other material on this page. (More details)
Users have permission to do anything with the code and other material on this page. (More details)
More information:
The code will plot any ellipse where you specify the major and minor axis distances, optional translation by \(x, y\) units, and an optional rotation. This revision deploys numpy broadcasting and numpy r_, and allows for a choice of 360*k + 1 for the number of points of each plot.
Expected result:

Tags:
aspect ratioellipse
Page views: 112 (past 60 days)
Identifier #: 23
Permalink to this revision: http://scpyce.org/23/2/
Permalink to latest revision: http://scpyce.org/23/