Skip to content

[Bug]: Polygon.contains_point() only works if transparency is zero #27026

Open
@adriandelser

Description

@adriandelser

Bug summary

When trying to see if a Polygon contains a point, the contains_point() method only seems to work if alpha is set to 0 or no options are set when defining the polygon. (mpl version = 3.8.0)

Code for reproduction

from matplotlib.patches import Polygon

vertices = [[0,0],[0,1],[1,1],[1,0]] # Square 
point = [0.5,0.5] # Point inside square

poly0 = Polygon(vertices, closed=True, fill=True, color=(1,0,0,1))  # False
poly1 = Polygon(vertices, closed=True, fill=True, color='r')        # False
poly2 = Polygon(vertices, closed=True, fill=True, color=(1,0,0,0))  # True
poly3 = Polygon(vertices, fill = False,alpha = 0)                   # True
poly4 = Polygon(vertices, fill = False)                             # False                       
poly5 = Polygon(vertices)                                           # True                       


for idx, poly in enumerate((poly0, poly1, poly2, poly3, poly4, poly5)):
    print(f"Polygon {idx} contains point {point}: {poly.contains_point(point)}")

Actual outcome

Polygon 0 contains point [0.5, 0.5]: False
Polygon 1 contains point [0.5, 0.5]: False
Polygon 2 contains point [0.5, 0.5]: True
Polygon 3 contains point [0.5, 0.5]: True
Polygon 4 contains point [0.5, 0.5]: False
Polygon 5 contains point [0.5, 0.5]: True

Expected outcome

Should return True for all polygons since the point is inside them all.

Additional information

Tested in mpl v3.7.1 and v3.8.0 with identical outcomes
Also fails if fill=False and alpha is not set.
Temporary fix is to set alpha to 0.

Operating system

MacOS 12.5.1 Monterrey

Matplotlib Version

3.8.0

Matplotlib Backend

MacOSX

Python version

Python 3.11.5

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions