Question

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'
Was it helpful?

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)

OTHER TIPS

Though pnpoly has been deprecated, your error was caused by a bug which has since been fixed in this commit on GitHub.

The error resulted from pnpoly proxying through to contains_point with the wrong method signature.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top