I came accross a bit of a problem and couldn't find a satisfying answer anywhere.
I'm looking to find the points contained between two curves, with the fact that both curves are kind of parametric, and don't have the same number of x-elements.
The matplotlib function fill
works perfectly to fill with polygons the area between the curves. I've stumbled upon the contains_point
function from matplotlib_path
, but I can't find a way to get the properties of the matplotlib.patches.Polygon
output from fill
to use it.
Is there a way to use this, or should I take another approach possible, by doing directly what the fill
function does to obtain the polygon (but how) ?
Plot illustating the problem:
For anyone interested, I managed to resolve my problem.
Using shapely
and descartes
(a simple pip install do the trick),
from shapely.geometry.polygon import Polygon
from shapely.geometry import Point
from descartes import PolygonPatch
you can create the needed polygon with Polygon
function by feeding it a tuple filed with x and y of both curves appended. You can create the polygon patch with the PolygonPatch
function from shapely and plot it using:
ax.add_patch(name_of_your_patch)
To check if a point is included inside the polygon, you can create a Point
object with shapely and the function:
name_of_your_polygon.contains(point)
will return a boolean.
Here is an example script (remember to import shapely
and descartes
):
main=rand(4,2)
main=tuple(map(tuple,main)) #Transform into tuple
poly=Polygon(main) #Create filling Polygon
poly_patch=PolygonPatch(poly) #Create Polygon Patch
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_patch(poly_patch) #Draws the patch
point=Point(0.5,0.5)
print(poly.contains(point))
ax.scatter(0.5,0.5,s=10000,color='gold',marker='+')
In the case I plotted below, the poly.contains(point)
function returns True
.