About path.Path.contains_point() with polygonal hole

This is my first post to this community.

I have one question about behavior about path.Path.contains_point().

from matplotlib.path import Path

# counter clockwise
v1 = [(2,2), (-2,2), (-2,-2), (2,-2), (2,2)]
# clockwise
v2 = [(1,1), (1,-1), (-1,-1), (-1,1), (1,1)]

codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]

path = Path(v1+v2, codes+codes)

When I plot this path, like below,

After creating this path,


my expected result is False, but returns True.

Is this behavior right?
Plz give me some helpful information :slight_smile:

This is a known limitation :cry:

See Unexpected behaviour with path.contains_points · Issue #2708 · matplotlib/matplotlib · GitHub and path.contains_points returns different points based on the direction of the path · Issue #9704 · matplotlib/matplotlib · GitHub for some discussion about thes problem.

1 Like

Thank you for your information!
I’ve resolved this by separating path each other and counting how many times the point is included by the paths.
Are there any pull requests or ideas about this issue?

Often the advice for these sort of problems is “use shapely instead”, e.g.

from shapely.geometry import Point, Polygon
polygon = Polygon(v1, holes=[v2])
print(polygon.contains(Point(0, 0)))

will give you the correct answer.

1 Like