1

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'
codingknob
  • 11,108
  • 25
  • 89
  • 126
  • 1
    I got this warning when I tested it `/home/tcaswell/local_installs/lib/python2.7/site-packages/matplotlib-1.3.x-py2.7-linux-x86_64.egg/matplotlib/nxutils.py:23: MatplotlibDeprecationWarning: nxutils is deprecated. Use matplotlib.path.Path.contains_point instead.` – tacaswell May 09 '13 at 01:25
  • but that shouldnt prevent it from working? – codingknob May 09 '13 at 18:28
  • I wish they would update their documentation. Grrr. Thank you for pointing this out. Appreciate it. – codingknob May 09 '13 at 19:03
  • You should create an issue about this (and suggest an improved example!). MPL is a big project with lots of moving parts, any help keep them up is appreciated. – tacaswell May 09 '13 at 20:17

2 Answers2

2

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)
codingknob
  • 11,108
  • 25
  • 89
  • 126
2

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.

aseagram
  • 1,201
  • 14
  • 18