There is a bug in PolygonInteractor -- the non-existent 'verts' is
reference. Note, there is a verts attribute in some of the inherited
Polygon classes, but not in the Polygon class itself. This is easy
to fix. Just remove the unnecessary line:
self.poly.verts = list(self.poly.verts)
and change all references of poly.verts to poly.xy.
This works fine for me. I found this while creating an interactive
polygon creator, attached for those interested. Feel free to use
this routine however you wish.
from matplotlib.patches import Polygon, PolygonInteractor
class PolyClick(object):
"""
p = PolyClick()
Interactive polygon creation, and editing. Create an instance, and start defining the
initial polynomial. At this point, vertecies are blue. Switch to editing mode by hitting
return. This changes the vertecies to red, and you can modify them as per PolygonInteractor.
(i.e., hit 'i' to insert a point, 'd' to delete a point, and 't' to toggle the vertex visibility)
Data are always available in p.x, p.y, and p.verts attributes.
"""
def _on_key(self, event):
if event.key is None:
# Get rid of line, and old mpl connections.
self._line.set_visible(False)
self._ax.figure.canvas.mpl_disconnect(self._key_id)
self._ax.figure.canvas.mpl_disconnect(self._click_id)
# start a polygon intteractor
verts = zip(self._xdata, self._ydata)
poly = Polygon(verts, alpha=0.2, fc='k')
self._ax.add_patch(poly)
self._pi = PolygonInteractor(poly)
self._ax.add_line(self._pi.line)
self.get_xdata = self._pi.line.get_xdata
self.get_ydata = self._pi.line.get_ydata
def _on_click(self, event):
self._xdata.append(event.xdata)
self._ydata.append(event.ydata)
self._line.set_data(self._xdata, self._ydata)
self._ax.figure.canvas.draw_idle()
def __init__(self, ax=None):
if ax is None: ax = pl.gca()
self._ax = ax
self._xdata = []; self._ydata = []
self._pi = None
self._line = pl.Line2D([],[],marker='o', markerfacecolor='b')
self._ax.add_line(self._line)
self._key_id = self._ax.figure.canvas.mpl_connect('key_press_event', self._on_key)
self._click_id = self._ax.figure.canvas.mpl_connect('button_press_event', self._on_click)
def get_xdata(self):
if self._pi is None:
return self._xdata
else:
return self._pi.line.get_xdata()
def get_ydata(self):
if self._pi is None:
return self._ydata
else:
return self._pi.line.get_ydata()
def get_verts(self):
return zip(self.get_xdata(), self.get_ydata())
x = property(get_xdata)
y = property(get_ydata)
verts = property(get_verts)
if __name__ == '__main__':
from numpy import *
import pylab as pl
import polygon as poly
fig = pl.figure()
ax = fig.add_subplot(111)
x, y = mgrid[0:11, 0:11]
xc, yc = mgrid[0.5:10.5, 0.5:10.5] # cell centers
q = random.rand(10, 10)
pl.pcolor(x, y, q)
pclick = PolyClick()
pl.show()
select_poly = poly.Polygon(pclick.verts)
selected = select_poly.inside(asarray(zip(xc.flatten(), yc.flatten()))).reshape(q.shape)
q = ma.masked_where(selected, q)
pl.pcolor(x, y, q)
pl.show()
#!/usr/bin/env python
# encoding: utf-8
"""Polygon geometry.
Copyright (C) 2006, Robert Hetland
Copyright (C) 2006, Stefan van der Walt
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import numpy as np
import string
import sys
import math
import scipy.weave as weave
def npnpoly(verts,points):
verts = verts.astype(np.float64)
points = points.astype(np.float64)
xp = np.ascontiguousarray(verts[:,0])
yp = np.ascontiguousarray(verts[:,1])
x = np.ascontiguousarray(points[:,0])
y = np.ascontiguousarray(points[:,1])
out = np.empty(len(points),dtype=np.uint8)
code = """
/* Code from:
http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
Copyright (c) 1970-2003, Wm. Randolph Franklin
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimers.
2. Redistributions in binary form must reproduce the above
copyright notice in the documentation and/or other materials
provided with the distribution.
3. The name of W. Randolph Franklin may not be used to endorse
or promote products derived from this Software without
specific prior written permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */
int i,j,n;
unsigned int c;
int nr_verts = Nxp[0];
for (n = 0; n < Nx[0]; n++) {
c = 0;
for (i = 0, j = nr_verts-1; i < nr_verts; j = i++) {
if ((((yp(i)<=y(n)) && (y(n)<yp(j))) ||
((yp(j)<=y(n)) && (y(n)<yp(i)))) &&
(x(n) < (xp(j) - xp(i)) * (y(n) - yp(i)) / (yp(j) - yp(i)) + xp(i)))
c = !c;
}
out(n) = c;
}
"""
weave.inline(code, ['xp','yp','x','y','out'],
type_converters=weave.converters.blitz)
return out
class Polygon:
"""
Polygon class
"""
def __init__(self, verts):
"""
Given xp and yp (both 1D arrays or sequences), create a new polygon.
p = Polygon(vertices) where vertices is an Nx2 array
p.inside(x, y) - Calculate whether points lie inside the polygon.
p.area() - The area enclosed by the polygon.
p.centroid() - The centroid of the polygon
"""
verts = np.atleast_2d(verts)
assert verts.shape[1] == 2, 'Vertices should be an Nx2 array, but is %s' % str(verts.shape)
assert len(verts) >= 3, 'Need 3 vertices to create polygon.'
# close polygon, if needed
if not np.all(verts[0]==verts[-1]):
verts = np.vstack((verts,verts[0]))
self.verts = verts
def __str__(self):
return 'Polygon: ' + string.join([str(vert) for vert in self.verts],', ')
def __repr__(self):
return "Polygon_" + str(len(self.verts)) + '_' + \
self.verts.__array_interface__['data'][0]
def inside(self,points):
"""Check whether given points are in the polygon.
points - Nx2 array
See http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
"""
verts = self.verts
points = np.atleast_2d(points)
assert points.shape[1] == 2, \
"Points should be of shape Nx2, is %s" % str(points.shape)
out = []
xpi = verts[:,0]
ypi = verts[:,1]
# shift
xpj = xpi[np.arange(xpi.size)-1]
ypj = ypi[np.arange(ypi.size)-1]
maybe = np.empty(len(xpi),dtype=bool)
for x,y in points:
maybe[:] = ((ypi <= y) & (y < ypj)) | ((ypj <= y) & (y < ypi))
out.append(sum(x < (xpj[maybe]-xpi[maybe])*(y - ypi[maybe]) \
/ (ypj[maybe] - ypi[maybe]) + xpi[maybe]) % 2)
return np.asarray(out,dtype=bool)
def inside(self,points):
return npnpoly(self.verts,points).astype(bool)
def area(self):
"""
Return the area of the polygon.
From Paul Bourke's webpage:
http://astronomy.swin.edu.au/~pbourke/geometry
"""
v = self.verts
v_first = v[:-1][:,[1,0]]
v_second = v[1:]
return np.diff(v_first*v_second).sum()/2.0
def centroid(self):
"Return the centroid of the polygon"
v = self.verts
a = np.diff(v[:-1][:,[1,0]]*v[1:])
area = a.sum()/2.0
return ((v[:-1,:] + v[1:,:])*a).sum(axis=0) / (6.0*area)
if __name__ == '__main__':
import pylab as pl
grid = np.mgrid[0:1:10j,0:1:10j].reshape(2,-1).swapaxes(0,1)
# simple area test
verts = np.array([[0.15,0.15],
[0.85,0.15],
[0.85,0.85],
[0.15,0.85]])
pa = Polygon(verts)
print pa.area()
print (0.85-0.15)**2
print pa
print pa.centroid()
# concave enclosure test-case for inside.
verts = np.array([[0.15,0.15],
[0.25,0.15],
[0.45,0.15],
[0.45,0.25],
[0.25,0.25],
[0.25,0.55],
[0.65,0.55],
[0.65,0.15],
[0.85,0.15],
[0.85,0.85],
[0.15,0.85]])
pb = Polygon(verts)
inside = pb.inside(grid)
pl.plot(grid[:,0][inside], grid[:,1][inside], 'g.')
pl.plot(grid[:,0][~inside], grid[:,1][~inside],'r.')
pl.plot(pb.verts[:,0],pb.verts[:,1], '-k')
print pb.centroid()
xc, yc = pb.centroid()
print xc, yc
pl.plot([xc], [yc], 'co')
pl.show()
# many points in a semicircle, to test speed.
grid = np.mgrid[0:1:100j,0:1:100j].reshape(2,-1).swapaxes(0,1)
xp = np.sin(np.arange(0,np.pi,0.01))
yp = np.cos(np.arange(0,np.pi,0.01))
pc = Polygon(np.hstack([xp[:,np.newaxis],yp[:,np.newaxis]]))
print "%d points inside %d vertex poly..." % (grid.size/2,len(verts)),
sys.stdout.flush()
inside = pc.inside(grid)
print "done."
pl.plot(grid[:,0][inside], grid[:,1][inside], 'g.')
pl.plot(grid[:,0][~inside], grid[:,1][~inside], 'r.')
pl.plot(pc.verts[:,0], pc.verts[:,1], '-k')
xc, yc = pc.centroid()
print xc, yc
pl.plot([xc], [yc], 'co')
pl.show()
----
Rob Hetland, Associate Professor
Dept. of Oceanography, Texas A&M University
http://pong.tamu.edu/~rob
phone: 979-458-0096, fax: 979-845-6331
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel