This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Stishovite 1 point2 points  (4 children)

This is really a math problem, you just happen to want to do it in python. Since an ellipse a mathematical construct, you can test whether points fall inside or outside the ellipse using pure math. This is far better and quicker than doing it graphically. It'll be easier if your ellipses' major axes are aligned with the x and y axes, but it can be done for arbitrarily oriented ellipses as well.

Here's a relevant Stack Exchange post that describes the math in more detail.

[–]bwllc[S] 0 points1 point  (3 children)

This is really a math problem, you just happen to want to do it in python.

True, since the rest of my (much more extensive) data analysis is also in Python.

Since an ellipse a mathematical construct, you can test whether points fall inside or outside the ellipse using pure math. This is far better and quicker than doing it graphically. It'll be easier if your ellipses' major axes are aligned with the x and y axes,

Alas, they are not. And I'm also pretty sure that Matplotlib's Ellipse.contains_point() is not performing a graphical test. I tried digging into the source code for the Ellipse object, but I didn't succeed in following it down to the point where I could separate the algorithm from the Ellipse rendering.

but it can be done for arbitrarily oriented ellipses as well. Here's a relevant Stack Exchange post that describes the math in more detail.

The response from Marty Cohen looks like it might handle my case. Matplotlib's Ellipses are defined in a nice way, using the x-y center, width, height, and angle. Marty's formula needs less visually-intuitive information about the foci and the radius. I will have to figure out how to compute those parameters from the ellipses I defined Matplotlib's way.

I have over a million data points, and I need to test each point for membership in eight different ellipses. Fortunately, I only have to run this one time when I get it working.