10

How do I draw an ellipse with shapely?

2 Answers 2

15

Answer

First, define a circle of radius 1.
Then define an ellipse along x and y axis.
Rotate the ellipse.
Finally, actually draw the ellipse.

Code

from matplotlib import pyplot
from shapely.geometry.point import Point
import shapely.affinity
from descartes import PolygonPatch
# Note: download figures.py manually from shapely github repo,
# put it in shapely install directory
from shapely.figures import SIZE, GREEN, GRAY, set_limits

# 1st elem = center point (x,y) coordinates
# 2nd elem = the two semi-axis values (along x, along y)
# 3rd elem = angle in degrees between x-axis of the Cartesian base
#            and the corresponding semi-axis
ellipse = ((0, 0),(7, 4),36)

# Let create a circle of radius 1 around center point:
circ = shapely.geometry.Point(ellipse[0]).buffer(1)

# Let create the ellipse along x and y:
ell  = shapely.affinity.scale(circ, int(ellipse[1][0]), int(ellipse[1][1]))

# Let rotate the ellipse (clockwise, x axis pointing right):
ellr = shapely.affinity.rotate(ell,ellipse[2])

# If one need to rotate it clockwise along an upward pointing x axis:
elrv = shapely.affinity.rotate(ell,90-ellipse[2])
# According to the man, a positive value means a anti-clockwise angle,
# and a negative one a clockwise angle.

# Plot the ellipse
fig = pyplot.figure()
ax = fig.add_subplot(111)
patch = PolygonPatch(elrv, fc=GREEN, ec=GRAY, alpha=0.5, zorder=2)
ax.add_patch(patch)
set_limits(ax, -10, 10, -10, 10)
pyplot.show()

Shapely documentation:
https://shapely.readthedocs.io/en/stable/manual.html

That's it!
Result of elrv:
drawn shapely ellipse

2

Different method that actually first uses matplotlib and then turns it into a shapely object. This is a little simpler, but the curves will be interpolated by line segments.

from matplotlib.patches import Ellipse
from shapely.geometry import Polygon 

# first draw the ellipse using matplotlib
ellipse = Ellipse((center_x, center_y), width, height, angle) 
vertices = ellipse.get_verts()     # get the vertices from the ellipse object
ellipse = Polygon(vertices)        # Turn it into a polygon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.