Search code examples
pythonmatplotlibattributeerrorpoint-in-polygon

matplotlib pnpoly example results in error


I ran the following code as per the example here:

http://matplotlib.org/faq/howto_faq.html#test-whether-a-point-is-inside-a-polygon

I would kindly appreciate your help. Thank you.

>>>import numpy as np
>>>import matplotlib.nxutils as nx
>>>verts = np.array([ [0,0], [0, 1], [1, 1], [1,0]], float)
>>>nx.pnpoly(0.5, 0.5, verts)

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\matplotlib\nxutils.py", line 26, in pnpoly
    return p.contains_point(x, y)
  File "C:\Python27\lib\site-packages\matplotlib\path.py", line 289, in contains_point
    transform = transform.frozen()
AttributeError: 'float' object has no attribute 'frozen'

>>>nx.pnpoly(0.5, 1.5, verts)

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\matplotlib\nxutils.py", line 26, in pnpoly
    return p.contains_point(x, y)
  File "C:\Python27\lib\site-packages\matplotlib\path.py", line 289, in contains_point
    transform = transform.frozen()
AttributeError: 'float' object has no attribute 'frozen'

Solution

  • A user on the matplotlib forum provided the following, which works as I tested:

    from matplotlib.path import Path
    path = Path(polygonVerts)
    isInside = path.contains_point(point)